我正在尝试确定使用python在命令行中执行某些操作的最佳方式。我已经用subprocess.Popen()在单个文件上实现了这一点。但是,我正在尝试确定最好的方法来处理大量不同的文件。我不确定是否应该创建一个批处理文件,然后在命令中执行,或者只是在代码中缺少了一些东西。我先向新手道歉。当我使用循环时,下面的脚本返回一个1的返回码,而不在循环中时返回0。对于手头的任务,最好的方法是什么?在def check_output(command, console):
if console == True:
process = subprocess.Popen(command)
else:
process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, universal_newlines=True)
output, error = process.communicate()
returncode = process.poll()
return returncode, output, error
for file in fileList.split(";"):
...code to create command string...
returncode, output, error = check_output(command, False)
if returncode != 0:
print("Process failed")
sys.exit()
编辑:命令字符串示例如下所示:
C:\路径\到\可执行文件.exe-C:\路径\到\输入.ext-o C:\路径\收件人\输出在