我想從這個修改我.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'語句中給文件對象一個名稱。 –