Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[Windows] Extract off-targets: opened temporary file cannot be moved #15

Open
jakeb1996 opened this issue Jun 21, 2023 · 0 comments · May be fixed by #20
Open

[Windows] Extract off-targets: opened temporary file cannot be moved #15

jakeb1996 opened this issue Jun 21, 2023 · 0 comments · May be fixed by #20

Comments

@jakeb1996
Copy link
Member

When running the extract off-targets script on Windows, an OS error may be thrown when trying to move the temporary file.

The temporary file is never closed and therefore, Windows does not want to move it.

The error occurs here:

shutil.move(mergedFile.name, fpOutput)

According to the Python docs, here, this behavior is expected:

tempfile.NamedTemporaryFile(mode='w+b', buffering=- 1, encoding=None, newline=None, suffix=None, prefix=None, dir=None, delete=True, *, errors=None)

This function operates exactly as TemporaryFile() does, except that the file is guaranteed to have a visible name in the file system (on Unix, the directory entry is not unlinked). That name can be retrieved from the name attribute of the returned file-like object. Whether the name can be used to open the file a second time, while the named temporary file is still open, varies across platforms (it can be so used on Unix; it cannot on Windows). If delete is true (the default), the file is deleted as soon as it is closed. The returned object is always a file-like object whose file attribute is the underlying true file object. This file-like object can be used in a with statement, just like a normal file.

One way to fix this is by closing the temporary file inside the while loop:

while len(sortedFiles) > 1:
# A file to write the merged sequences to
mergedFile = tempfile.NamedTemporaryFile(delete = False)
# Select the files to merge
while True:
try:
sortedFilesPointers = [open(file, 'r') for file in sortedFiles[:maxNumOpenFiles]]
break
except OSError as e:
if e.errno == 24:
printer(f'Attempted to open too many files at once (OSError errno 24)')
maxNumOpenFiles = max(1, int(maxNumOpenFiles / 2))
printer(f'Reducing the number of files that can be opened by half to {maxNumOpenFiles}')
continue
raise e
printer(f'Merging {len(sortedFilesPointers):,}')
# Merge and write
with open(mergedFile.name, 'w') as f:
f.writelines(heapq.merge(*sortedFilesPointers))
# Close all of the open files
for file in sortedFilesPointers:
file.close()
# prepare for the next set to be merged
sortedFiles = sortedFiles[maxNumOpenFiles:] + [mergedFile.name]
shutil.move(mergedFile.name, fpOutput)

mergedFile.close()
@jakeb1996 jakeb1996 changed the title [Windows] Opened temporary file cannot be moved [Windows] Extract off-targets: opened temporary file cannot be moved Jun 21, 2023
@jakeb1996 jakeb1996 linked a pull request Jan 11, 2024 that will close this issue
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant