[英]How to 'catch' stdin, stdout, stderr of a program in Python?
[英]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
等)設置的參數被分配了作為要執行命令的一部分的值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.