所以我正在处理以下代码。当我的Reff.txt有多行时,它顺利运行。但是当我的Reff.txt文件有一行时,它不起作用。这是为什么?我也想知道为什么我的代码不运行我的代码的“尝试”部分,但它总是只运行“异常”部分。
但是,此代码在我的“try:”部分中没有做任何事情
import sys
import urllib2
from lxml import etree
import os
getReference = open('Reff.txt','r') #open the file that contains list of reference ids
global tID
for tID in getReference:
tID = tID.strip()
try:
with open(''+tID.strip()+'.txt') as f: pass
fileInput = open(''+tID+'.txt','r')
readAA = fileInput.read()
store_value = (readAA.partition('\n'))
aaSequence = store_value[2].replace('\n', '') #concatenate lines
makeList = list(aaSequence)#print makeList
inRange = ''
fileAddress = '/database/int/data/'+tID+'.txt'
filename = open(fileAddress,'r')#name of the working file
print fileAddress
with open(fileAddress,'rb') as f:
root = etree.parse(f)
for lcn in root.xpath("/protein/match[@dbname='PFAM']/lcn"):#find dbname =PFAM
start = int(lcn.get("start"))#if it is PFAM then look for start value
end = int(lcn.get("end"))#if it is PFAM then also look for end value
while start <= end:
inRange = makeList[start]
start += 1
print outputFile.write(inRange)
outputFile.close()
break
break
break
except IOError as e:
newURL ='http://www.uniprot.org/uniprot/'+tID+'.fasta'
print newURL
response = urllib2.urlopen(''+newURL) #go to the website and grab the information
creatNew = open(''+uniprotID+'.txt','w')
html = response.read() #read file
creatNew.write(html)
creatNew.close()
答案 0 :(得分:1)
因此,当您执行Try / Except时 - 如果尝试失败,则运行除外。除了总是在运行,因为Try总是失败。
最可能的原因是你有这个 - “print outputFile.write(inRange)”,但你之前没有声明过outputFile。
ETA:此外,您似乎只对测试for循环的第一遍感兴趣?你在这一点上打破了。在那种情况下,你的其他休息是无关紧要的,因为当那个休息时,它们永远不会被触及。