我如何在Python中命名一个外部命令,就好像我把它写在一个<unk>或命令中?
当前回答
有很多不同的图书馆,允许您使用Python呼叫外部命令. 对于每个图书馆,我给了一个描述,并显示了一个例子呼叫外部命令. 我使用的命令作为例子是ls -l(列出所有文件)。 如果你想了解更多关于任何图书馆,我列出并链接到文件的每一个。
来源
这些都是图书馆
希望这会帮助你做出决定哪个图书馆使用:)
副过程
subprocess.run(["ls", "-l"]) # Run command
subprocess.run(["ls", "-l"], stdout=subprocess.PIPE) # This will run the command and return any output
subprocess.run(shlex.split("ls -l")) # You can also use the shlex library to split the command
os 用于“操作系统依赖功能”。 它也可以用来呼叫与 os.system 和 os.popen 的外部命令(注:也有 subprocess.popen)。 os 总是会运行盾牌,并且对于那些不需要或不知道如何使用 subprocess.run 的人来说是一个简单的替代方案。
os.system("ls -l") # Run command
os.popen("ls -l").read() # This will run the command and return any output
饰 sh
sh 是一个子过程界面,允许您呼叫程序,就好像它们是功能。
sh.ls("-l") # Run command normally
ls_cmd = sh.Command("ls") # Save command as a variable
ls_cmd() # Run command as if it were a function
铅
ls_cmd = plumbum.local("ls -l") # Get command
ls_cmd() # Run command
佩克斯
pexpect.run("ls -l") # Run command as normal
child = pexpect.spawn('scp foo user@example.com:.') # Spawns child application
child.expect('Password:') # When this is the output
child.sendline('mypassword')
织物
fabric.operations.local('ls -l') # Run command as normal
fabric.operations.local('ls -l', capture = True) # Run command and receive output
发送
r = envoy.run("ls -l") # Run command
r.std_out # Get output
命令
其他回答
import subprocess
p = subprocess.run(["ls", "-ltr"], capture_output=True)
print(p.stdout.decode(), p.stderr.decode())
网上尝试
与标准图书馆
使用子过程模块(Python 3):
import subprocess
subprocess.run(['ls', '-l'])
但是,更复杂的任务(管道,输出,输入等)可以是无聊的构建和写作。
注意 Python 版本: 如果您仍然使用 Python 2, subprocess.call 以类似的方式运行。
ProTip: shlex.split 可以帮助您打破运行命令、呼叫和其他子过程功能,如果您不希望(或您不能!)以列表的形式提供它们:
import shlex
import subprocess
subprocess.run(shlex.split('ls -l'))
与外部依赖
如果你不注意外部依赖,使用铅:
from plumbum.cmd import ifconfig
print(ifconfig['wlan0']())
它是最好的子处理器. 它是跨平台,即它在Windows和Unix类似的系统上运行。
另一个受欢迎的图书馆是:
from sh import ifconfig
print(ifconfig('wlan0'))
然而,sh下降了Windows支持,所以它不像它以前那样令人惊叹。
您可以尝试使用 os.system() 运行外部命令。
例子:
import os
try:
os.system('ls')
pass
except:
print("Error running command")
pass
在示例中,脚本输入OS并试图运行在OS.system()中列出的命令。如果命令失败,那么它将打印“错误运行命令”而没有脚本因错误而停止。
是的,只是如此简单!
我会建议使用子过程模块而不是os.system,因为它会让您逃脱,因此更安全。
subprocess.call(['ping', 'localhost'])
从 OpenStack Neutron 中获取网络 ID:
#!/usr/bin/python
import os
netid = "nova net-list | awk '/ External / { print $2 }'"
temp = os.popen(netid).read() /* Here temp also contains new line (\n) */
networkId = temp.rstrip()
print(networkId)
新网列表
+--------------------------------------+------------+------+
| ID | Label | CIDR |
+--------------------------------------+------------+------+
| 431c9014-5b5d-4b51-a357-66020ffbb123 | test1 | None |
| 27a74fcd-37c0-4789-9414-9531b7e3f126 | External | None |
| 5a2712e9-70dc-4b0e-9281-17e02f4684c9 | management | None |
| 7aa697f5-0e60-4c15-b4cc-9cb659698512 | Internal | None |
+--------------------------------------+------------+------+
印刷输出(networkId)
27a74fcd-37c0-4789-9414-9531b7e3f126
推荐文章
- 证书验证失败:无法获得本地颁发者证书
- 当使用pip3安装包时,“Python中的ssl模块不可用”
- 无法切换Python与pyenv
- Python if not == vs if !=
- 如何从终端/命令行调用VS代码编辑器
- 如何从scikit-learn决策树中提取决策规则?
- 为什么在Mac OS X v10.9 (Mavericks)的终端中apt-get功能不起作用?
- 将旋转的xtick标签与各自的xtick对齐
- 为什么元组可以包含可变项?
- 如何合并字典的字典?
- 如何创建类属性?
- 不区分大小写的“in”
- 在Python中获取迭代器中的元素个数
- 解析日期字符串并更改格式
- 查看PS命令的全部输出