2017-05-25 31 views
-1
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         ^

錯誤表示字符串的結尾......我不知道這可能是什麼語法錯誤。

任何幫助,這將不勝感激

+0

提示:'-a'是一個Python變量嗎? – chepner

回答

1

從文檔,它看起來像論據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