Skip to content

Commit 160758a

Browse files
authored
gh-126565: Skip zipfile.Path.exists check in write mode (#126576)
When `zipfile.Path.open` is called, the implementation will check whether the path already exists in the ZIP file. However, this check is only required when the ZIP file is in read mode. By swapping arguments of the `and` operator, the short-circuiting will prevent the check from being run in write mode. This change will improve the performance of `open()`, because checking whether a file exists is slow in write mode, especially when the archive has many members.
1 parent 450db61 commit 160758a

File tree

2 files changed

+2
-1
lines changed

2 files changed

+2
-1
lines changed

Lib/zipfile/_path/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ def open(self, mode='r', *args, pwd=None, **kwargs):
339339
if self.is_dir():
340340
raise IsADirectoryError(self)
341341
zip_mode = mode[0]
342-
if not self.exists() and zip_mode == 'r':
342+
if zip_mode == 'r' and not self.exists():
343343
raise FileNotFoundError(self)
344344
stream = self.root.open(self.at, zip_mode, pwd=pwd)
345345
if 'b' in mode:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve performances of :meth:`zipfile.Path.open` for non-reading modes.

0 commit comments

Comments
 (0)