gpt4 book ai didi

python - 将 Python 2.7 中使用子进程的代码转换为 Python 2.3

转载 作者:行者123 更新时间:2023-11-28 17:51:28 25 4
gpt4 key购买 nike

我正在编写一个 python 程序,它将生成一个子进程来基本上克隆一个日志文件。我在 2.7 中有工作代码,但由于供应商限制,我不得不使用 Python 2.3,所以使用 subprocess 模块是不可能的

我需要帮助翻译:

self.FILE_OUT =  open(self.file2write,"a" )
self.FILE_IN=subprocess.Popen( self.file2process,
stdout=self.FILE_OUT,
bufsize=0,
shell=True)

进入 2.3 代码。

我对如何执行 stdout=self.FILE_OUT 感到困惑。

最佳答案

这取决于你在做什么,但执行命令的旧方法是使用 os.popen。它是这样工作的:

>>> echo_stdout = os.popen('echo foo', 'r')
>>> echo_stdout.read()
'foo\n'

os 模块中还有其他版本的 popen 可以做一些事情,比如返回一个包含 stdinstdout 的二元组,或包含 stdinstdoutstderr 的三元组。查看docs了解更多详细信息,如果这没有帮助,请告诉我。

这是一个如何使用 popen2 的例子:

>>> cat_stdin, cat_stdout = os.popen2('cat')
>>> cat_stdin.write('foo\n')
>>> cat_stdin.close()
>>> cat_stdout.read()
'foo\n'

关于python - 将 Python 2.7 中使用子进程的代码转换为 Python 2.3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9082935/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com