在 Python 2.7 中使用标准输入、标准输出和标准错误的正确方法是什么?

What is the correct way to use stdin, stdout, and stderr in Python 2.7?

有没有人有在Python中使用subprocess.call命令的经验? 每当我的代码中有这样一行时,我就会不断收到错误消息:

INFILE1 = open(script_dir+"/Scripts/plot_TSS_profile.R","r")
  subprocess.call("Rscript","--slave","--args",filenames["housekeeping_profile"]+" "+filenames["unexpressed_profile"]+" "+filenames["profile_plot"],stdin=INFILE1,  stderr=ERR_LOG,stdout=OUT_LOG,shell=True)
   INFILE1.close(). 

如果我按原样保留代码,我会收到一个错误,即程序出于某种原因为每个 stdin、stderr 和 stdout 找到多个值,即使这些是代码中唯一的值。如果我取出这些参数并将 infile 放在“--args”之后的括号中,它似乎不会读取文件,因为它说“缓冲区应该是一个整数”。

比如这种方式给出的缓冲区错误:

INFILE1 = script_dir+"/Scripts/plot_TSS_profile.R"
    subprocess.call("Rscript",INFILE1,"--slave","--args",filenames["housekeeping_profile"]+" "+filenames["unexpressed_profile"]+" "+filenames["profile_plot"],stderr=ERR_LOG,shell=True)
    INFILE1.close()

以下是我的错误输出以获取更具体的信息:

buffsize 上的那个:

回溯(最近调用最后): 文件“/mnt/work1/users/pughlab/projects/IEG_MiSEQ/Inferring_DNA_Expression/ExpressionPrediction-master/expression_prediction.py”,第 277 行,在

    step5(ERR_LOG,OUT_LOG,args,proj_dir,script_dir,filenames)
  File "/mnt/work1/users/pughlab/projects/IEG_MiSEQ/Inferring_DNA_Expression/ExpressionPrediction-master/expression_prediction.py", line 165, in step5
    subprocess.call("Rscript",INFILE1,"--slave","--args",filenames["housekeeping_profile"]+" "+filenames["unexpressed_profile"]+" "+filenames["profile_plot"],stderr=ERR_LOG,shell=True)
  File "/mnt/work1/software/centos7/python/2.7.15/lib/python2.7/subprocess.py", line 172, in call
    return Popen(*popenargs, **kwargs).wait()
  File "/mnt/work1/software/centos7/python/2.7.15/lib/python2.7/subprocess.py", line 343, in __init__
    raise TypeError("bufsize must be an integer")
TypeError: bufsize must be an integer

还有关于存在多个值的另一个错误:

 Traceback (most recent call last):
  File "/mnt/work1/users/pughlab/projects/IEG_MiSEQ/Inferring_DNA_Expression/ExpressionPrediction-master/expression_prediction.py", line 272, in <module>
    step5(ERR_LOG,OUT_LOG,args,proj_dir,script_dir,filenames)
  File "/mnt/work1/users/pughlab/projects/IEG_MiSEQ/Inferring_DNA_Expression/ExpressionPrediction-master/expression_prediction.py", line 165, in step5
    subprocess.call("Rscript","--slave","--args",filenames["housekeeping_profile"]+" "+filenames["unexpressed_profile"]+" "+filenames["profile_plot"],stdin=INFILE1,stderr=ERR_LOG,stdout=OUT_LOG,shell=True)
  File "/mnt/work1/software/centos7/python/2.7.15/lib/python2.7/subprocess.py", line 172, in call
    return Popen(*popenargs, **kwargs).wait()
TypeError: __init__() got multiple values for keyword argument 'stdin'

谢谢

主要问题是 call 接受一个 列表 个参数或一个 单个 字符串由 shell。您在这里似乎不需要 shell=True;只需创建一个包含您尝试传递的所有位置参数的列表。

with open(script_dir+"/Scripts/plot_TSS_profile.R","r") as INFILE1:
    cmd = [
        "Rscript",
        "--slave",
        "--args",
        filenames["housekeeping_profile"], 
        filenames["unexpressed_profile"],
        filenames["profile_plot"]
    ]
    subprocess.call(cmd, stdin=INFILE1, stderr=ERR_LOG, stdout=OUT_LOG)

传递多个位置参数意味着本应通过关键字参数(如 stdin 等)设置的参数正在被分配值,这些值本应作为要执行的命令的一部分。