Python 将控制台输出导出为.txt不会';行不通

Python 将控制台输出导出为.txt不会';行不通,python,bash,subprocess,Python,Bash,Subprocess,我正在尝试将控制台输出从Script1.py保存到.txt文件。 但是,我需要为几个参数运行这个脚本,例如“pythonscript1.py43131”,其中“43131”是参数,参数存储在一个列表中(runnumer)。 我现在尝试执行另一个脚本“WrapperScript1.py”,使用典型的bash导出为我执行以下操作: from subprocess import call for i in range(len(Runnummer)): call(["python Scr

我正在尝试将控制台输出从Script1.py保存到.txt文件。 但是,我需要为几个参数运行这个脚本,例如“pythonscript1.py43131”,其中“43131”是参数,参数存储在一个列表中(runnumer)。 我现在尝试执行另一个脚本“WrapperScript1.py”,使用典型的bash导出为我执行以下操作:

from subprocess import call
for i in range(len(Runnummer)):    
    call(["python Script1.py " + Runnummer[i] + 
          ' > ' + './test/Run' + Runnummer[i] +'.txt'])
这段代码现在应该执行“pythonscript1.pyarg(i)>./test/runarg(i).txt”。 我在控制台中手动尝试了一个I,它可以工作,但是如果我使用subprocess并在其上循环,它就不起作用了。 发生的情况是代码正常运行,但没有控制台输出保存到.txt

我读到你们也可以从子流程中使用管道,但我真的不知道如何使用它,所以我像上面一样尝试过。我也尝试过os.system,但它也不起作用


提前谢谢

假设您事先知道要运行循环的次数,您可以使用shell而不是从另一个python脚本调用一个python脚本:

for i in {0..100}; do python Script1.py $i > test/Run$i.txt; done
如前所述(感谢@tripleee),
{0..100}
范围是一个Bash特性,因此这不会在所有shell中都起作用。如果您的shell不支持大括号扩展,您可以使用
seq
工具
for i in$(seq 0 100)
,如果不支持,可以使用
while
循环:

i=0
while [ $i -le 100 ]; do
    python Script1.py $i > test/Run$i.txt
    i=$((i+1)) # POSIX compliant (thanks @chepner)
    # or, for a more vintage experience
    # i=$(expr $i + 1)
done

假设您事先知道要运行循环的次数,则可以使用shell,而不是从另一个python脚本调用一个python脚本:

for i in {0..100}; do python Script1.py $i > test/Run$i.txt; done
如前所述(感谢@tripleee),
{0..100}
范围是一个Bash特性,因此这不会在所有shell中都起作用。如果您的shell不支持大括号扩展,您可以使用
seq
工具
for i in$(seq 0 100)
,如果不支持,可以使用
while
循环:

i=0
while [ $i -le 100 ]; do
    python Script1.py $i > test/Run$i.txt
    i=$((i+1)) # POSIX compliant (thanks @chepner)
    # or, for a more vintage experience
    # i=$(expr $i + 1)
done

第一件事是
call
需要一个参数数组

第二件事是
call
不要重定向为shell,这样就不能使用

对于收集子流程的输出,更简单的方法是使用
检查\u输出

from subprocess import check_output
Runnummer=["a","b"]
for i in range(len(Runnummer)):    
    with open('./test/Run' + Runnummer[i] +'.txt',"w") as output:
        output.write(check_output(["python","Script1.py",str(Runnummer[i])]))
从pythonic风格的角度来看,95%的时间不需要
range
,只需直接在列表上迭代即可。因此:

from subprocess import check_output
Runnummer=["c","d"]
for run in Runnummer:    
    with open('./test/Run' + run +'.txt',"wb") as output:
        output.write(check_output(["python","Script1.py",run]))

第一件事是
call
需要一个参数数组

第二件事是
call
不要重定向为shell,这样就不能使用

对于收集子流程的输出,更简单的方法是使用
检查\u输出

from subprocess import check_output
Runnummer=["a","b"]
for i in range(len(Runnummer)):    
    with open('./test/Run' + Runnummer[i] +'.txt',"w") as output:
        output.write(check_output(["python","Script1.py",str(Runnummer[i])]))
从pythonic风格的角度来看,95%的时间不需要
range
,只需直接在列表上迭代即可。因此:

from subprocess import check_output
Runnummer=["c","d"]
for run in Runnummer:    
    with open('./test/Run' + run +'.txt',"wb") as output:
        output.write(check_output(["python","Script1.py",run]))

您可以使用
os.system
而不是
子流程

import os
for i in range(len(Runnummer)):
    os.system('python Script1.py ' + Runnummer[i] + 
              ' > ' + './test/Run' + Runnummer[i] +'.txt')

您可以使用
os.system
而不是
子流程

import os
for i in range(len(Runnummer)):
    os.system('python Script1.py ' + Runnummer[i] + 
              ' > ' + './test/Run' + Runnummer[i] +'.txt')

重定向是一种shell功能。如果要使用它,
shell
参数需要设置为
True

此外,您混合了两种调用约定。要么传递一个字符串供shell解析,要么将已解析的标记列表作为字符串传递

from subprocess import call
for i in range(len(Runnummer)):    
    call("python Script1.py " + Runnummer[i] + 
      ' > ' + './test/Run' + Runnummer[i] +'.txt', shell=True)

由于您无论如何都要调用shell,因此在shell脚本中执行此操作可能更有意义,正如中所建议的那样。

重定向是一种shell功能。如果要使用它,
shell
参数需要设置为
True

此外,您混合了两种调用约定。要么传递一个字符串供shell解析,要么将已解析的标记列表作为字符串传递

from subprocess import call
for i in range(len(Runnummer)):    
    call("python Script1.py " + Runnummer[i] + 
      ' > ' + './test/Run' + Runnummer[i] +'.txt', shell=True)

由于您无论如何都要调用shell,因此在shell脚本中执行此操作可能更有意义,如中所述。

与其在shell中使用I/O重定向,不如打开一个文件以用Python编写,并使用
stdout
参数将文件句柄传递给
调用

from subprocess import call
for f in Runnummer:
    output_file = "./test/Run{0}.txt".format(f)
    with open(output_file, "w") as fh:
        call(["python", "Script1.py", f], stdout=fh)

此外,直接在列表上迭代比在用作列表索引的整数列表上迭代更为简洁。

不要在shell中使用I/O重定向,打开一个文件以用Python编写,并使用
stdout
参数将文件句柄传递给
调用

from subprocess import call
for f in Runnummer:
    output_file = "./test/Run{0}.txt".format(f)
    with open(output_file, "w") as fh:
        call(["python", "Script1.py", f], stdout=fh)

此外,直接在列表上迭代比在用作列表索引的整数列表上迭代更为简洁。

参数列表是如何确定的?这已经在python中完成了,还是可以使用shell?参数列表是如何确定的?这是在python中已经完成的,还是可以只使用shell?请注意,大括号表达式是一个Bash扩展。如果您想要或需要使用
/bin/sh
,一种便携式解决方案是使用
$(seq 1 100)
。(不过,
seq
的可用性也不是完全通用的。)@tripleee感谢编辑,我对比我老的shell的了解非常有限:)
expr
是不必要的
i=$((i+1))
由POSIX.Heh“vintage”支持。如果您想在
bash
中看到一个真正晦涩难懂的蒸汽朋克(过时的),请尝试
i=$[i+1]
:)非常感谢,这很有效!同时也非常感谢其他试图帮忙的人!:)请注意,大括号表达式是Bash扩展。如果您想要或需要使用
/bin/sh
,一种便携式解决方案是使用
$(seq 1 100)
。(不过,
seq
的可用性也不是完全通用的。)@tripleee感谢编辑,我对比我老的shell的了解非常有限:)
expr
是不必要的
i=$((i+1))
由POSIX.Heh“vintage”支持。如果您想在
bash
中看到一个真正晦涩难懂的蒸汽朋克(过时的),请尝试
i=$[i+1]
:)非常感谢,这很有效!同时也非常感谢其他试图帮忙的人!:)即使是
os
模块的文档也建议在
os.system
上使用
subprocess
。即使是
os
模块的文档也建议在
os.system
上使用
subprocess