我将首先展示我目前拥有的代码:
def err(em):
print(em)
exit
def rF(f):
s = ""
try:
fh = open(f, 'r')
except IOError:
e = "Could not open the file: " + f
err(e)
try:
with fh as ff:
next(ff)
for l in ff:
if ">" in l:
next(ff)
else:
s += l.replace('\n','').replace('\t','').replace('\r','')
except:
e = "Unknown Exception"
err(e)
fh.close()
return s
出于某种原因,每当我尝试通过键入以下内容读取文件时,python shell(我使用的是 3.2.2)都会冻结:
rF("mycobacterium_bovis.fasta")
rF 函数中的条件是防止读取以“>”标记开头的每一行。这些行不是 DNA/RNA 代码(这是我试图从这些文件中读取的),应该被忽略。
我希望任何人都可以帮助我解决这个问题,我没有看到我的错误。
像往常一样,提前非常感谢!
编辑: *问题仍然存在!* 这是我现在使用的代码,我删除了无论如何都是一个花哨的添加的错误处理,当尝试读取文件时,shell 仍然冻结。这是我现在的代码:
def rF(f):
s = ""
try:
fh = open(f, 'r')
except IOError:
print("Err")
try:
with fh as ff:
next(ff)
for l in ff:
if ">" in l:
next(ff)
else:
s += l.replace('\n','').replace('\t','').replace('\r','')
except:
print("Err")
fh.close()
return s