levitsky / pyteomics

Pyteomics is a collection of lightweight and handy tools for Python that help to handle various sorts of proteomics data. Pyteomics provides a growing set of modules to facilitate the most common tasks in proteomics data analysis.
http://pyteomics.readthedocs.io
Apache License 2.0
115 stars 35 forks source link

After using ms_data = mzxml.MzXML(filename) the file stays open on Windows. #25

Closed sorenwacker closed 3 years ago

sorenwacker commented 3 years ago

I wonder if I have to call some method to close the file? On Linux I did not experience any trouble, but on Windows processed files cannot be deleted.

I open the file with

ms_data = mzxml.MzXML(fn)

And then iterate through the data to generate a pandas dataframe. Then the data is stored in a different format.

while True:
        try:
            data = ms_data.next()     
            df = pd.DataFrame({col: np.array(data[col]) for col in cols} )
            slices.append( df )
        except:         
            break 

How can I close the file again? I looked in the documentation, but did not find a related method.

levitsky commented 3 years ago

Hi! This aspect is consistent with how you work with file objects.

Accordingly, there is a .close() method on all readers. You should be able to do:

ms_data.close()

Alternatively, you can use the with syntax, e.g.:

with mzxml.MzXML(fn) as ms_data:
    for data in ms_data:
         df = pd.DataFrame({col: np.array(data[col]) for col in cols})
         slices.append( df )

This will close the file automatically.

levitsky commented 3 years ago

Hi @soerendip, was I able to answer your question?

sorenwacker commented 3 years ago

Yes, thank you. I had not seen the .close() method in the docs, but it makes total sense :-)

levitsky commented 3 years ago

Thanks! For reference, the .close() method is mentioned in the docs here: https://pyteomics.readthedocs.io/en/latest/data.html#general-notes.