如何使用子进程在python循环中运行shell程序?
问题描述:
num_of_files = 12
pair_count = 1
while pair_count < num_of_files:
for root, dirs, all_files in os.walk(indir):
read1 = all_files[pair_count]
pair_count += 1
read2 = all_files[pair_count]
print(read1, read2)
pair_count += 1
process = subprocess.Popen
(cutadapt -a AATGATACGGCGACCACCGAGATCTACACGCCTCCCTCGCGCCATCAG
-o out1.fastq -p out.2fastq read1 read2, shell=True, stdout=subprocess.PIPE)
if pair_count > num_of_files:
break
我似乎无法在我的python循环中使用cutadapt shell脚本。它给当我运行了以下错误信息:如何使用子进程在python循环中运行shell程序?
process = subprocess.Popen(cutadapt -a AATGATACGGCGACCACCGAGATCTACACGCCTCCCTCGCGCCATCAG
-o out1.fastq -p out.2fastq read1 read2, shell=True, stdout=subprocess.PIPE)
SyntaxError: invalid syntax ^
错误表示字符串的结尾......我不知道这可能是什么语法错误。
任何帮助,这将不胜感激
答
从文档,它看起来像论据subprocess.Popen
应弦的字符串或列表。
是否
subprocess.Popen(["cutadapt", "-a", "AATGATACGGCGACCACCGAGATCTACACGCCTCCCTCGCGCCATCAG", "-o", "out1.fastq", "-p", "out.2fastq", "read1", "read2"], shell=True, stdout=subprocess.PIPE)
提供所需的呼叫?
从文档:
>>> import shlex, subprocess
>>> command_line = raw_input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print args
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
请特别注意,选项(如 - 输入)和由空格在外壳分隔参数(如eggs.txt)在单独的列表中的元素去,而在shell中使用时需要引用或反斜杠转义的参数(如包含空格的文件名或上面显示的echo命令)是单列表元素。
https://docs.python.org/2/library/subprocess.html#popen-constructor
+0
啊哈,我会试试看。非常感谢! – ainebrowne
提示:'-a'是一个Python变量吗? – chepner