简体   繁体   English

在python中bash终端的命令

[英]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.

相关问题 在Python中调用bash命令返回错误,但可在终端中使用 - Calling bash command inside Python returns error, but works in terminal OS X和Python 3:在新终端中执行bash命令时的行为? - OS X & Python 3: Behavior while executing bash command in new terminal? Bash curl 命令在终端中有效,但不适用于 Python os.system() - Bash curl command works in terminal, but not with Python os.system() Python命令输出终端 - Python Command Output Terminal 命令在python中失败,但在终端中未失败 - Command fails in python, but not in terminal Python 在终端中添加命令 - Python add command in terminal 如何使用带有stdin的wall命令向python中的所有bash终端广播消息? - How do I broadcast messages to all bash terminal in python using wall command with stdin? 命令在bash终端中运行,但在Python subprocess中不起作用。 获得“路径必须在表达式之前:'%p'错误 - Command working in bash terminal but not in Python subprocess.Popen(); getting a 'paths must precede expression: `%p' error 有没有办法在执行下一个命令之前等待(在 bash 脚本中)python 程序(在新终端中启动)结束? - Is there a way to wait (in a bash script) for a python program (started in a new terminal) to end before executing the next command? 命令在终端或bash脚本中运行正常,但在使用子流程模块的python脚本中运行不正常 - Command works fine from terminal or inside bash script but not from python script using subprocess module
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM