zudi-lin / pytorch_connectomics

PyTorch Connectomics: segmentation toolbox for EM connectomics
http://connectomics.readthedocs.io/
MIT License
169 stars 77 forks source link

CondSeg Explicitly Close File #136

Open Lauenburg opened 9 months ago

Lauenburg commented 9 months ago

Describe the bug In the following line, the file opened in the list comprehension is not explicitly closed. In Python, it's generally good practice to open files using the with statement, which ensures that the file is properly closed after its suite finishes, even if an exception is raised. Not closing files can lead to memory leaks or issues with file locks, especially in larger applications or if the code is run multiple times in a loop.

filelist = [line.rstrip('\n') for line in open(name)]

Expected behavior

The file is closed automatically. A better approach would be:

with open(name) as file:
    filelist = [line.rstrip('\n') for line in file]