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

标签 python command-line-interface

我有许多 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

我可以在这里使用这个还是有更好的方法?我问是因为我目前正在阅读 subprocess 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"])

关于python - 使用python批量运行其他python脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17377113/

相关文章:

python - beautifulsoup中属性为中文时如何获取标签

python - 用闭包替换简单的 Python 类是否有益?

从 crond 启动时出现 PHP 段错误

python - 在多个Python进程之间共享RabbitMQ channel

python - 组合 __setattr__ 和 __getattr__ 会导致无限循环

python - 使用 Functional API 和 tf.GradientTape() 的组合在 Tensorflow 2.0 中训练时如何将模型图记录到张量板?

azure - az vm open-port icmp,这可能吗?

javascript - 在可读事件监听器回调中读取时,Node 的 process.stdin 可读流记录 Null

linux - 在 Ubuntu Server (Linux) 中使用 rm 时如何更改错误消息

java - 我可以在没有 - 标志的情况下使用 Commons CLI 命令行吗?