【发布时间】:2010-01-04 08:36:46
【问题描述】:
我正在编写一个 python 脚本来替换具有特定扩展名 (.seq) 的目录中每个文本文件中的字符串。替换的字符串应该只来自每个文件的第二行,并且输出是一个新的子目录(称为 clean),其文件名与原始文件相同,但带有 *.clean 后缀。输出文件包含与原始文件完全相同的文本,但替换了字符串。我需要用 'N' 替换所有这些字符串:'K','Y','W','M','R','S'。
这是我在谷歌搜索后想出的。它非常混乱(编程的第二周),它停止将文件复制到干净的目录中而不替换任何东西。非常感谢任何帮助。
之前谢谢!
import os, shutil
os.mkdir('clean')
for file in os.listdir(os.getcwd()):
if file.find('.seq') != -1:
shutil.copy(file, 'clean')
os.chdir('clean')
for subdir, dirs, files in os.walk(os.getcwd()):
for file in files:
f = open(file, 'r')
for line in f.read():
if line.__contains__('>'): #indicator for the first line. the first line always starts with '>'. It's a FASTA file, if you've worked with dna/protein before.
pass
else:
line.replace('M', 'N')
line.replace('K', 'N')
line.replace('Y', 'N')
line.replace('W', 'N')
line.replace('R', 'N')
line.replace('S', 'N')
【问题讨论】: