Skip to main content
 首页 » 编程设计

python之使用python批量运行其他python脚本

2024年11月01日7bluestorm

我有一些 python 脚本,我想通过管道传输大约 1000 次,为每个脚本更改输入文件

我之前使用 bash shell 脚本来执行此操作,但现在我需要它才能在 Windows 机器上运行。

这是 python,有问题的行被注释掉了

namecount = 0 
for file in files: 
     in_filestring = "test_" + str(namecount) 
     out_filestring = "out_ + str(namecount) 
     namecount += 1 
     #Run this on the command line: python pre.py < in_filestring | filter.py | a_filter.py > out_filestring 

我可以在这里使用它还是有更好的方法?我问是因为我目前正在阅读子流程 http://docs.python.org/2/library/subprocess.html .显然它取代了过时的 os.system,但我还不知道如何使用它。

import os 
os.system('system command you want to run') 

请您参考如下方法:

subprocess.call 应该没问题。基本是,

call(["args" in comma separated]) 

这是链接http://docs.python.org/2/library/subprocess.html#using-the-subprocess-module .

在你的情况下,尝试这样的事情,

from subprocess import call 
... 
... 
call(["python", "pre.py", "<", filestring, "|", "filter.py", "|", "a_filter.py", ">", "out_filestring"])