我想从这个修改我.fasta文件编辑文件夹中的多个文件:开幕式,并与蟒蛇
>YP_009208724.1 hypothetical protein ADP65_00072 [Achromobacter phage phiAxp-3]
MSNVLLKQ...
>YP_009220341.1 terminase large subunit [Achromobacter phage phiAxp-1]
MRTPSKSE...
>YP_009226430.1 DNA packaging protein [Achromobacter phage phiAxp-2]
MMNSDAVI...
这样:
>Achromobacter phage phiAxp-3
MSNVLLKQ...
>Achromobacter phage phiAxp-1
MRTPSKSE...
>Achromobacter phage phiAxp-2
MMNSDAVI...
现在,我已经有一个脚本,可以做到一个单一的文件:
with open('Achromobacter.fasta', 'r') as fasta_file:
out_file = open('./fastas3/Achromobacter.fasta', 'w')
for line in fasta_file:
line = line.rstrip()
if '[' in line:
line = line.split('[')[-1]
out_file.write('>' + line[:-1] + "\n")
else:
out_file.write(str(line) + "\n")
但我不能自动化过程中的所有120个文件在我的文件夹。
我使用glob.glob试过,但我似乎无法使其工作:
import glob
for fasta_file in glob.glob('*.fasta'):
outfile = open('./fastas3/'+fasta_file, 'w')
with open(fasta_file, 'r'):
for line in fasta_file:
line = line.rstrip()
if '[' in line:
line2 = line.split('[')[-1]
outfile.write('>' + line2[:-1] + "\n")
else:
outfile.write(str(line) + "\n")
它给了我这样的输出:
A
c
i
n
e
t
o
b
a
c
t
e
r
.
f
a
s
t
a
我设法让所有的列表文件夹中的文件,但无法使用列表中的对象打开某些文件。
import os
file_list = []
for file in os.listdir("./fastas2/"):
if file.endswith(".fasta"):
file_list.append(file)
在第二代码片段,你迭代的文件名,而不是文件:'在fasta_file'线。您需要在'with'语句中给文件对象一个名称。 –