Python 用于编写文件的shell程序的subprocess.call()

Python 用于编写文件的shell程序的subprocess.call(),python,shell,popen,Python,Shell,Popen,我需要使用python执行一个shell脚本。shell程序的输出是一个文本文件。没有对脚本的输入。帮我解决这个问题 def invokescript( shfile ): s=subprocess.Popen(["./Script1.sh"],stderr=subprocess.PIPE,stdin=subprocess.PIPE); return; invokescript("Script1.sh"); 在使用上述代码时,我收到以下错误 Traceback (most recen

我需要使用python执行一个shell脚本。shell程序的输出是一个文本文件。没有对脚本的输入。帮我解决这个问题

def invokescript( shfile ):
  s=subprocess.Popen(["./Script1.sh"],stderr=subprocess.PIPE,stdin=subprocess.PIPE);
  return;

invokescript("Script1.sh");
在使用上述代码时,我收到以下错误

Traceback (most recent call last):
  File "./test4.py", line 12, in <module>
    invokescript("Script1.sh");
  File "./test4.py", line 8, in invokescript
    s=subprocess.Popen(["./Script1.sh"],stderr=subprocess.PIPE,stdin=subprocess.PIPE);
  File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
    errread, errwrite)
  File "/usr/lib/python2.7/subprocess.py", line 1249, in _execute_child
    raise child_exception
OSError: [Errno 8] Exec format error
回溯(最近一次呼叫最后一次):
文件“/test4.py”,第12行,在
invokescript(“Script1.sh”);
文件“/test4.py”,invokescript中的第8行
s=subprocess.Popen([“/Script1.sh”],stderr=subprocess.PIPE,stdin=subprocess.PIPE);
文件“/usr/lib/python2.7/subprocess.py”,第679行,在__
错误读取,错误写入)
文件“/usr/lib/python2.7/subprocess.py”,第1249行,在执行子进程中
引发子对象异常
OSError:[Errno 8]Exec格式错误
提前感谢…

试试这个:

import shlex

def invokescript(shfile):
    return subprocess.Popen(
        shlex.split(shfile),
        stderr=subprocess.PIPE,
        stdin=subprocess.PIPE
    )

invokescript("Script1.sh");
并添加
#/usr/bin/env bash
当然可以添加到您的bash文件中。

我使用os.system()调用shell脚本。这是我所期望的。确保在python代码中导入了os模块

invokescript( "Script1.sh" ) // Calling Function

function invokescript( shfile ):  // Function Defenition
     os.system("/root/Saranya/Script1.sh")
     return;
以下内容也是可执行的:

invokescript( "Script1.sh" ) // Calling Function

function invokescript( shfile ):  // Function Defenition
     os.system(shfile)
     return;

谢谢你们的及时回复,伙计们

可能重复:pythonITYM
s=subprocess.Popen([shfile],…)
中不需要分号。否则你就不需要这个参数了。顺便说一句,你应该返回这个
s
,以防调用方需要它。我认为shebang就是这里的解决方案。调用
shlex.split()
有什么意义?这里没有要拆分的内容…好的,我编辑了代码。我添加了“shlex.split”,因为函数现在更通用了。但我仍然面临同样的错误。谢谢你的回复