我在AIX 6.1上运行并使用Python 2.7。要执行以下行但出现错误。
subprocess.run(["ls", "-l"]) Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: 'module' object has no attribute 'run'
该subprocess.run()函数仅在Python 3.5及更高版本中存在。
subprocess.run()
但是,向后移植很容易:
def run(*popenargs, **kwargs): input = kwargs.pop("input", None) check = kwargs.pop("handle", False) if input is not None: if 'stdin' in kwargs: raise ValueError('stdin and input arguments may not both be used.') kwargs['stdin'] = subprocess.PIPE process = subprocess.Popen(*popenargs, **kwargs) try: stdout, stderr = process.communicate(input) except: process.kill() process.wait() raise retcode = process.poll() if check and retcode: raise subprocess.CalledProcessError( retcode, process.args, output=stdout, stderr=stderr) return retcode, stdout, stderr
没有为超时的支持,并为完成过程信息没有自定义类,所以我只能返回retcode,stdout和stderr信息。否则,它会执行与原始文件相同的操作。
retcode
stdout
stderr