python3 Oracle 中的子进程 Linux (wget -o)

python3 subprocess in Oracle Linux (wget -o)

我看到有几篇关于 python 子进程调用 bash shell 命令的帖子。但是我找不到我的问题的答案,除非有人 link 我错过了。

我的代码从这里开始。

import os;
import subprocess;
    subprocess.call("wget ‐O /home/oracle/Downloads/puppet-repo.rpm https://yum.puppetlabs.com/puppetlabs-release-el-6.noarch.rpm");

当我做的时候

wget ‐O /home/oracle/Downloads/puppet-repo.rpm https://yum.puppetlabs.com/puppetlabs-release-el-6.noarch.rpm

直接在终端中运行,它有效。

但是我的 IDE 给了我 FileNotFoundError: [Errno 2] No such file or directory: 'wget'

同样,我是在 python 中调用 os/subprocess 模块的新手,如果您能提供有关如何有效使用这些模块的任何见解,我将不胜感激。

{更新:根据 miindlek 的回答,我得到了这些错误。第一 - subprocess.call(["wget", "‐O", "/home/oracle/Downloads/puppet-repo.rpm", "https://yum.puppetlabs.com/puppetlabs-release-el-6.noarch.rpm"])}

--2015-06-07 17:14:37--  http://%E2%80%90o/
Resolving ‐o... failed: Temporary failure in name resolution.
wget: unable to resolve host address “‐o”
/home/oracle/Downloads/puppet-repo.rpm: Scheme missing.
--2015-06-07 17:14:52--  https://yum.puppetlabs.com/puppetlabs-release-el-6.noarch.rpm

{使用第二种 bash 方法 subprocess.call("wget ‐O /home/oracle/Downloads/puppet-repo.rpm https://yum.puppetlabs.com/puppetlabs-release-el-6.noarch.rpm", shell=True)}

Resolving yum.puppetlabs.com... 198.58.114.168, 2600:3c00::f03c:91ff:fe69:6bf0
Connecting to yum.puppetlabs.com|198.58.114.168|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 10184 (9.9K) [application/x-redhat-package-manager]
Saving to: “puppetlabs-release-el-6.noarch.rpm.1”

     0K .........                                             100% 1.86M=0.005s

2015-06-07 17:14:53 (1.86 MB/s) - “puppetlabs-release-el-6.noarch.rpm.1” saved [10184/10184]

FINISHED --2015-06-07 17:14:53--
Downloaded: 1 files, 9.9K in 0.005s (1.86 MB/s)

Process finished with exit code 0

您应该将命令字符串拆分为参数列表:

import subprocess
subprocess.call(["wget", "-O", "/home/oracle/Downloads/puppet-repo.rpm", "https://yum.puppetlabs.com/puppetlabs-release-el-6.noarch.rpm"])

您也可以使用 shell 选项作为替代:

import subprocess
subprocess.call("wget -O /home/oracle/Downloads/puppet-repo.rpm https://yum.puppetlabs.com/puppetlabs-release-el-6.noarch.rpm", shell=True)

顺便说一句,在 python 中,您不需要在行尾添加分号。

更新 选项 -O 中的破折号是 utf8 连字符字符,而不是破折号。参见示例:

>>> a = "‐"  # utf8 hyphen
>>> b = "-"  # dash
>>> str(a)
'\xe2\x80\x9'
>>> str(b)
'-'

您应该删除旧的破折号,然后用普通的破折号代替。我更新了以前的源代码。您也可以从那里复制它。

这听起来主要是因为您的 IDE 正在从 而不是 'straight up in a terminal' 启动 python 子进程。

这将是一个阅读建议,而不是仅针对此问题的直接答案。

检查你的IDE;阅读有关它如何启动内容的文档。

1 - 在终端中 在你测试的地方输入 $ env $ wget

2 - 在 IDE import os ; print(os.environ)

3 - 在此处阅读有关 shell 和 Popen 的信息 https://docs.python.org/3/library/subprocess.html

从那里开始学习过程。

我什至建议更换

subprocess.call("wget -O /home/oracle/Downloads/puppet-repo.rpm https://yum.puppetlabs.com/puppetlabs-release-el-6.noarch.rpm", shell=True)

明确声明 'shell' 你想使用什么

subprocess.Popen(['/bin/sh', '-c', 'wget' '<stuff>'])

减轻未来 IDE/shell/env 假设问题。