Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7588029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:49:22+00:00 2026-05-30T19:49:22+00:00

I am going to start of by showing the code I have thus far:

  • 0

I am going to start of by showing the code I have thus far:

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

For some reason the python shell (I am using 3.2.2) freezes up whenever I tried to read a file by typing:

rF("mycobacterium_bovis.fasta")

The conditionals in the rF function are to prevent reading each line that starts with a “>” token. These lines aren’t DNA/RNA code (which is what I am trying to read from these files) and should be ignored.

I hope anyone can help me out with this, I don’t see my error.

As per the usual, MANY thanks in advance!

EDIT:
*The problem persists!*
This is the code I now use, I removed the error handling which was a fancy addition anyway, still the shell freezes whenever attempting to read a file. This is my code now:

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T19:49:23+00:00Added an answer on May 30, 2026 at 7:49 pm

    You didn’t ever define e.
    So you’ll get a NameError that is being hidden by the naked except:.

    This is why it is good and healthy to specify the exception, e.g.:

    try: 
        print(e)
    except NameError as e: 
        print(e)
    

    In cases like yours, though, when you don’t necessarily know what the exception will be you should at least use this method of displaying information about the error:

    import sys
    try:
        print(e)
    except: # catch *all* exceptions
        e = sys.exc_info()[1]
        print(e)
    

    Which, using the original code you posted, would have printed the following:

    name 'e' is not defined
    

    Edit based on updated information:
    Concatenating a string like that is going to be quite slow if you have a large file.
    Consider instead writing the filtered information to another file, e.g.:

    def rF(f):
      with open(f,'r') as fin, open('outfile','w') as fou:
        next(fin)
        for l in fin:
          if ">" in l:
            next(fin)
          else:
            fou.write(l.replace('\n','').replace('\t','').replace('\r',''))
    

    I have tested that the above code works on a FASTA file based on the format specification listed here: http://en.wikipedia.org/wiki/FASTA_format using Python 3.2.2 [GCC 4.6.1] on linux2.

    A couple of recommendations:

    • Start small. Get a simple piece working then add a step.
    • Add print() statements at trouble spots.

    Also, consider including more information about the contents of the file you’re attempting to parse. That may make it easier for us to help.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am going to start on a new project for my school. I have
I was going to start using === (triple equals, strict comparison) all the time
I am going to start working on a website that has already been built
We are going to start a long lasting project using C++ as the programming
I'm going to start working on project developed in ASP.NET. In this project I
I am going to start a project of my own, which will be ASP.NET
So we start Scrum today and start going over story points estimates. The first
I'm going to start a new project that's building web apps from scratch. I
I am going to start a new project based on the top of MongoDB.
I am going to start a new online trading simulation game. Server Page: A

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.