我正在尝试修改我的.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...
为此:
^{pr2}$现在,我已经有了一个可以对单个文件执行此操作的脚本:
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个文件。在
我试着用环球网,但我似乎没办法做到:
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)
考虑到您现在能够更改文件名的内容,您需要自动化该过程。我们通过删除两次打开文件时使用的文件处理程序来更改一个文件的函数。在
现在我们需要遍历你所有的文件。因此,让我们使用
^{pr2}$glob
模块您正在迭代文件名,这将为您提供名称中的所有字符,而不是文件的行。以下是代码的更正版本:
作为Python脚本的替代方法,您只需从命令行使用
^{pr2}$sed
:这将修改所有文件,因此请考虑先复制它们。在
相关问题 更多 >
编程相关推荐