[英]Command to bash terminal in python
I've used the methods in this answer to try to send "du -sch *" to the terminal but I get errors. 我已使用此答案中的方法尝试将“ du -sch *”发送到终端,但出现错误。
For example, when I use this: 例如,当我使用这个时:
out = check_output(["du", "-sch", "*"])
I get: 我得到:
Traceback (most recent call last):
File "test_main.py", line 29, in test_sample
out = check_output(["du", "-sch", "*"])
File "/usr/local/lib/python2.7/subprocess.py", line 575, in check_output
raise CalledProcessError(retcode, cmd, output=output)
CalledProcessError: Command '['du', '-sch', '*']' returned non-zero exit status 1
The problem I get is due to the "*" because without it it works fine. 我遇到的问题是由于“ *”,因为没有它,它可以正常工作。
I've also tried: 我也尝试过:
output = subprocess.Popen(["du", "-sch", "*"], stdout=subprocess.PIPE, shell=True).communicate()[0]
But I get a different result than the one I get using the command directly in the terminal. 但是我得到的结果不同于直接在终端中使用命令得到的结果。
from subprocess import check_output
out = check_output("du -sch *", shell=True)
More about shell argument : 有关shell参数的更多信息:
The shell argument (which defaults to False) specifies whether to use the shell as the program to execute.
shell参数(默认为False)指定是否将shell用作要执行的程序。 If shell is True, it is recommended to pass args as a string rather than as a sequence.
如果shell为True,建议将args作为字符串而不是序列传递。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.