我是非常新的python,我有一個python腳本運行特定文件(input1.txt)並生成一個輸出(output1.fasta),但我想運行該腳本的多個文件,例如:input2.txt,input3.txt ......併產生相應的輸出:output2.fasta,output3.fasta修改python腳本運行多個輸入文件
from Bio import SeqIO
fasta_file = "sequences.txt"
wanted_file = "input1.txt"
result_file = "output1.fasta"
wanted = set()
with open(wanted_file) as f:
for line in f:
line = line.strip()
if line != "":
wanted.add(line)
fasta_sequences = SeqIO.parse(open(fasta_file),'fasta')
with open(result_file, "w") as f:
for seq in fasta_sequences:
if seq.id in wanted:
SeqIO.write([seq], f, "fasta")
我嘗試添加水珠功能,但我不知道如何處理輸出文件名。
from Bio import SeqIO
import glob
fasta_file = "sequences.txt"
for filename in glob.glob('*.txt'):
wanted = set()
with open(filename) as f:
for line in f:
line = line.strip()
if line != "":
wanted.add(line)
fasta_sequences = SeqIO.parse(open(fasta_file),'fasta')
with open(result_file, "w") as f:
for seq in fasta_sequences:
if seq.id in wanted:
SeqIO.write([seq], f, "fasta")
的錯誤信息是:NameError:名字「result_file」沒有定義
究竟是什麼「不工作」?你可以使用glob嘗試後顯示你的代碼嗎? – Verv
什麼不適用於glob?具體,以便我們可以提供幫助。 – kabanus
對不起,我更新了錯誤信息等問題。 – Paul