【发布时间】:2012-07-10 16:07:22
【问题描述】:
所以我正在处理下面的代码。当我的 Reff.txt 有不止一行时,它符合要求。但是当我的 Reff.txt 文件只有一行时它不起作用。这是为什么?我还想知道为什么我的代码不运行我的代码的“尝试”部分,但它总是只运行“异常”部分。
- 所以我有一个参考文件,其中包含一个 id 列表(每行一个 id)
- 我使用参考文件 (Reff.txt) 作为参考来搜索网站中的数据库和我网络中服务器中的数据库。
- 我应该得到的结果是应该有一个输出文件和包含该 id 信息的文件;对于每个参考 ID
但是,这段代码在我的“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()
【问题讨论】:
-
你的 with 块是怎么回事?它立即通过...
-
把它从 try except 块中取出,看看你得到了什么错误......猜测文件不存在
-
为什么每个区块后面都有一个无条件的
break? -
另外,
filename = open(有点误导,因为它不是名称,它是一个对象,您可以直接使用etree.parse('filename')而无需显式打开句柄 -
如果您解释一下您所拥有的以及您想要实现的目标(可能是要点),这可能会有所帮助 - 我正在努力遵循代码和您的意图......跨度>
标签: python exception try-catch