捕获 运行 期间由 Python 中的子进程生成的内容
catch contents generated during run by subprocess in Python
我想捕获在 运行 期间生成的内容而不是输出,因为我意识到程序的输出不幸不是有用的信息。
基本上我的代码:
command = "usearch11 -threads 8 -fastq_filter pmFLP.fastq -fastq_maxee 1 -fastq_minlen 120 -fastq_maxns 0 -fastaout S1--1.QC.fasta"
with Popen(command, stdout=PIPE, stderr=None, shell=True) as process:
output = process.stdout.readlines()
print(output)
以及终端中显示的内容 after/during 运行输入代码:
C:\Users\Desktop\Daten\test_Ordner>python test.py
00:00 4.6Mb FASTQ base 33 for file pmFLP.fastq
00:01 5.3Mb 100.0% Filtering, 98.6% passed
188650 Reads (188.7k)
1297 Discarded read with > 0 Ns
1132 Discarded reads with expected errs > 1.00
185957 Filtered reads (186.0k, 98.6%)
b'usearch v11.0.667_win64, 137Gb RAM, 64 cores\r\n(C) Copyright 2013-18 Robert C. Edgar, all rights reserved.\r\nhttps://drive5.com/usearch\r\n\r\nLicense: kleine@planton.de\r\n\r\n'
这部分正是我需要的:
00:00 4.6Mb FASTQ base 33 for file pmFLP.fastq
00:01 5.3Mb 100.0% Filtering, 98.6% passed
188650 Reads (188.7k)
1297 Discarded read with > 0 Ns
1132 Discarded reads with expected errs > 1.00
185957 Filtered reads (186.0k, 98.6%)
任何帮助将不胜感激!!
您可能需要捕捉 stderr
而不是 stdout
。我还建议使用 subprocess.run()
而不是 subprocess.Popen()
:
command = "usearch11 -threads 8 -fastq_filter pmFLP.fastq -fastq_maxee 1 -fastq_minlen 120 -fastq_maxns 0 -fastaout S1--1.QC.fasta"
completed_process = run(command, stderr=PIPE, shell=True, text=True)
desired_text = completed_process.stderr
print(desired_text)
如果您想要原始字节,请删除 text=True
标志:
command = "usearch11 -threads 8 -fastq_filter pmFLP.fastq -fastq_maxee 1 -fastq_minlen 120 -fastq_maxns 0 -fastaout S1--1.QC.fasta"
completed_process = run(command, stderr=PIPE, shell=True)
desired_text = completed_process.stderr # Those are now bytes, not str!
print(desired_text)
将输出写入二进制文件:
command = "usearch11 -threads 8 -fastq_filter pmFLP.fastq -fastq_maxee 1 -fastq_minlen 120 -fastq_maxns 0 -fastaout S1--1.QC.fasta"
with open("summary.txt", "wb") as file:
run(command, stderr=file, shell=True)
如您所见,您还可以将任何可写文件对象传递给 stderr
或 stdout
。
我想捕获在 运行 期间生成的内容而不是输出,因为我意识到程序的输出不幸不是有用的信息。
基本上我的代码:
command = "usearch11 -threads 8 -fastq_filter pmFLP.fastq -fastq_maxee 1 -fastq_minlen 120 -fastq_maxns 0 -fastaout S1--1.QC.fasta"
with Popen(command, stdout=PIPE, stderr=None, shell=True) as process:
output = process.stdout.readlines()
print(output)
以及终端中显示的内容 after/during 运行输入代码:
C:\Users\Desktop\Daten\test_Ordner>python test.py
00:00 4.6Mb FASTQ base 33 for file pmFLP.fastq
00:01 5.3Mb 100.0% Filtering, 98.6% passed
188650 Reads (188.7k)
1297 Discarded read with > 0 Ns
1132 Discarded reads with expected errs > 1.00
185957 Filtered reads (186.0k, 98.6%)
b'usearch v11.0.667_win64, 137Gb RAM, 64 cores\r\n(C) Copyright 2013-18 Robert C. Edgar, all rights reserved.\r\nhttps://drive5.com/usearch\r\n\r\nLicense: kleine@planton.de\r\n\r\n'
这部分正是我需要的:
00:00 4.6Mb FASTQ base 33 for file pmFLP.fastq
00:01 5.3Mb 100.0% Filtering, 98.6% passed
188650 Reads (188.7k)
1297 Discarded read with > 0 Ns
1132 Discarded reads with expected errs > 1.00
185957 Filtered reads (186.0k, 98.6%)
任何帮助将不胜感激!!
您可能需要捕捉 stderr
而不是 stdout
。我还建议使用 subprocess.run()
而不是 subprocess.Popen()
:
command = "usearch11 -threads 8 -fastq_filter pmFLP.fastq -fastq_maxee 1 -fastq_minlen 120 -fastq_maxns 0 -fastaout S1--1.QC.fasta"
completed_process = run(command, stderr=PIPE, shell=True, text=True)
desired_text = completed_process.stderr
print(desired_text)
如果您想要原始字节,请删除 text=True
标志:
command = "usearch11 -threads 8 -fastq_filter pmFLP.fastq -fastq_maxee 1 -fastq_minlen 120 -fastq_maxns 0 -fastaout S1--1.QC.fasta"
completed_process = run(command, stderr=PIPE, shell=True)
desired_text = completed_process.stderr # Those are now bytes, not str!
print(desired_text)
将输出写入二进制文件:
command = "usearch11 -threads 8 -fastq_filter pmFLP.fastq -fastq_maxee 1 -fastq_minlen 120 -fastq_maxns 0 -fastaout S1--1.QC.fasta"
with open("summary.txt", "wb") as file:
run(command, stderr=file, shell=True)
如您所见,您还可以将任何可写文件对象传递给 stderr
或 stdout
。