0

I have a python function that contains the following code:

with open(modelfilepath, "rb") as modelfile, open(vcffilepath, "rb") as vcffile:
    for row in gtf_getrow(modelfile):
        print row
        #add features as appropriate
        if row["feature"] == "transcript":
            addfeature(some args...)
        if row["feature"] == "exon":
            addfeature(some other args..., vcffile=vcffile)

Execution of the addfeature() function passes through several functions before returning to the for loop. In the "exon" case, the vcffile object is passed as an argument to successive functions which eventually write to the vcffile.

The problem is that after a few iterations, the vcffile object seems to close spontaneously, which crashes the program. If I hardcode the function that uses vcffile to access the filename directly, the problem does not occur, but this seems like an undesirable solution since it removes control of the file from the with block. Nor do I want to have to open and close the file each time I access it, since this program is parsing hundreds of megabytes worth of tabular data. Thanks in advance for your suggestions.

Eicos
  • 35
  • 7
  • 1
    This code is fine; the problem must lie elsewhere. What is your OS and file system? Is the file across the network? – Janne Karila Mar 07 '13 at 09:35
  • I don't see anything wrong with the code you show. Can you come up with an SSCCE: http://sscce.org/ – NPE Mar 07 '13 at 09:45
  • 3
    Also, are you sure you're not accidentally closing the file yourself in one of the nested functions? – NPE Mar 07 '13 at 09:46
  • Put some debug statements in to see when the file is open, and when it closes. – danodonovan Mar 07 '13 at 10:09
  • Thanks for your eyes. I did attach a debugger and stepped up and down the stack on the last pre-crash op a few times, but never managed to figure out quite what was going on. It actually was crashing in the gzip module. But I noticed that I was creating a reader object from the vcffile object inside a loop rather than outside, resulting in repeated calls to the __init__, so once I moved that out of the loop the problem went away. – Eicos Mar 09 '13 at 00:20

0 Answers0