BioGeek
BioGeek

Reputation: 23

Unable to run function with exception

I have a text file with a list of codes, and I want to write a script that creates folders named after each code and applies a function ncbiSeq() to each code that writes a file and stores it in its respective folder.

The script works properly for non-previously exisiting folders.

To avoid an error if the folder named after one of the codes already exists, I tried using try and except. However, I am ending up with an empty folder for this specific code (I am unable to apply my function to this code).

Below is my code:

import os
with open('list.txt') as list:
    for line in list:
        code =line.strip('\n')
        try:
            os.mkdir(code)
        except:
            continue
        ncbiSeq(code)

NB: This is my first time using try and except.

Upvotes: 1

Views: 93

Answers (2)

Snild Dolkow
Snild Dolkow

Reputation: 6866

It's not very clear from its name, but the continue keyword means "go to the next iteration of the loop". So, when you get your exception, you won't run the ncbiSeq(code) line.

If you change it to pass instead (which is simply a python line that does nothing), I think your program will work as expected.

Upvotes: 0

Davi
Davi

Reputation: 428

Use pass instead of continue. continue ignores the rest of the current iteration and starts another one. You are ignoring your function call and starting a new iteration. pass, instead, just does nothing. It explicitly tells the interpreter that there is nothing missing, you really don't want to do anything there.

Upvotes: 1

Related Questions