TIM*_*MEX 30 python unix linux bash shell
import subprocess
retcode = subprocess.call(["/home/myuser/go.sh", "abc.txt", "xyz.txt"])
Run Code Online (Sandbox Code Playgroud)
当我运行这两行时,我会这样做吗?:
/home/myuser/go.sh abc.txt xyz.txt
Run Code Online (Sandbox Code Playgroud)
为什么我会收到此错误?但是当我正常运行go.sh时,我没有得到那个错误.
File "/usr/lib/python2.6/subprocess.py", line 480, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.6/subprocess.py", line 633, in __init__
errread, errwrite)
File "/usr/lib/python2.6/subprocess.py", line 1139, in _execute_child
raise child_exception
OSError: [Errno 8] Exec format error
Run Code Online (Sandbox Code Playgroud)
Joh*_*web 33
OSError:[Errno 8] Exec格式错误
这是操作系统在尝试运行时报告的错误/home/myuser/go.sh.
它看起来像shebang(#!)行go.sh是无效的.
这是一个从shell运行的示例脚本,但不是从Popen:
#\!/bin/sh
echo "You've just called $0 $@."
Run Code Online (Sandbox Code Playgroud)
\从第一行删除可以解决问题.
jbp*_*jbp 10
将代码更改为以下内容:
retcode = subprocess.call(["/home/myuser/go.sh", "abc.txt", "xyz.txt"], shell=True,)
Run Code Online (Sandbox Code Playgroud)
注意"shell = True"
来自:http://docs.python.org/library/subprocess.html#module-subprocess
在Unix上,shell = True:如果args是一个字符串,它指定要通过shell执行的命令字符串.这意味着字符串的格式必须与在shell提示符下键入时完全相同.
是的,如果您所做的只是调用 shell 脚本、等待其完成并收集其退出状态,同时让其 stdin、stdout 和 stderr 从您的 Python 进程继承,那就完全没问题了。如果您需要对这些因素中的任何一个进行更多控制,那么您只需使用更通用的subprocess.Popen,但除此之外您所拥有的就可以了。