try:和exception:

2024-04-19 19:52:20 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我在写下面的代码。当我的孩子参考文件.txt有多行。但当我的孩子参考文件.txt文件只有一行。为什么?我还想知道为什么我的代码不运行代码的“try”部分,但它总是只运行“exception”部分。你知道吗

  • 所以我有一个引用文件,它有一个id列表(每行一个id)
  • 我使用参考文件(参考文件.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()

Tags: 文件the代码importtxtidforvalue
1条回答
网友
1楼 · 发布于 2024-04-19 19:52:20

所以,当你尝试/除外-如果尝试失败,除外运行。除了总是在运行,因为尝试总是失败的。你知道吗

最可能的原因是你有这个“印刷品”outputFile.write文件(inRange)”,但您以前没有声明outputFile。你知道吗

ETA:而且,看起来您只对测试for循环的第一次通过感兴趣?你在那一点上就断了。在这种情况下,你的其他休息时间是无关的,因为当那个休息时间在那里的时候,他们永远不会到达。你知道吗

相关问题 更多 >