所以我正在处理下面的代码。当我的 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()