Open ok-oldking opened 4 months ago
You can read the suggestion what AI explained the situation.
Even though your program calls close()
on a file, there are a few reasons why Windows (and sometimes other operating systems) might not release the lock right away:
What happens: When a program writes to a file, Windows may not write the data to disk immediately. It temporarily stores it in a buffer to optimize performance.
Impact: The file might appear locked for a short time, even after closing, as Windows finishes the background work (like flushing the buffer).
Solution: You can use flush()
before closing the file to force immediate write to disk.
with open("example.txt", "w") as f:
f.write("Hello, World!")
f.flush() # Ensures everything is written immediately.
# The file will still be properly closed and unlocked.
What happens: Some antivirus or backup tools monitor files when they are opened or written to. After your program closes the file, the antivirus might still be holding the lock while scanning.
Impact: Even though your program is done, the antivirus causes the file to remain temporarily locked.
Solution: Try disabling or excluding the file from antivirus scans during development to see if it resolves the issue.
f1 = open("example.txt", "w")
f2 = open("example.txt", "r") # Oops! Now two handles are open.
f1.close() # Only one is closed; the other is still open.
# The file is still locked because f2 is open.
with
statements to ensure all files are properly closed automatically.flush()
if you’re writing and want immediate disk writes.with
statements to safely manage file operations.By keeping these points in mind, you can avoid file-locking issues and ensure your programs behave as expected on Windows!
This may be happened because SevenZipFile
raises the exception when opening a given corrupted file.
SevenZipFile
class defines
def __exit__(self, exc_type, exc_val, exc_tb):
self.close()
def _fpclose(self) -> None:
assert self._fileRefCnt > 0
self._fileRefCnt -= 1
if not self._fileRefCnt and not self._filePassed:
self.fp.close()
def close(self):
"""Flush all the data into archive and close it.
When close py7zr start reading target and writing actual archive file.
"""
if "w" in self.mode:
self._write_flush()
if "a" in self.mode:
self._write_flush()
if "r" in self.mode:
if self.reporterd is not None:
self.q.put_nowait(None)
self.reporterd.join(1)
if self.reporterd.is_alive():
raise InternalError("Progress report thread terminate error.")
self.reporterd = None
self._fpclose()
self._var_release()
SevenZipFile
class supports a multithreaded extraction, so when another working process exists, it does not close immediately. It may be a reason.
assert self._fileRefCnt > 0
self._fileRefCnt -= 1
if not self._fileRefCnt and
This part may cause the unclosed status.
When I’m extracting a particular corrupted 7z file, I get the PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: ‘corrupt.7z’. This error only occurs with this corrupted file. When I test with other random invalid 7z files, the error does not occur. Because the file is 80MB, I can't upload it to github, here is the google drive link