New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
zipfile.Path.iterdir() outputs sub directories many times or not at all #81953
Comments
Hello, #!/usr/bin/python3.8 from zipfile import ZipFile, Path
import io
def recurse_print(parent):
for child in parent.iterdir():
if child.is_file():
print(child, child.read_text())
if child.is_dir():
recurse_print(child)
data = io.BytesIO()
zf = ZipFile(data, "w")
zf.writestr("a.txt", "content of a")
zf.writestr("b/c.txt", "content of c")
zf.writestr("b/d/e.txt", "content of e")
zf.writestr("b/f.txt", "content of f")
zf.filename = "abcde.zip"
root = Path(zf)
recurse_print(root) Expected result: abcde.zip/a.txt content of a Actual result: abcde.zip/a.txt content of a Path._add_implied_dirs adds the sub directory "b/" twice: once for each direct child (i.e. "c.txt" and "f.txt") And similarly: data = io.BytesIO()
zf = ZipFile(data, "w")
zf.writestr("a.txt", "content of a")
zf.writestr("b/d/e.txt", "content of e")
zf.filename = "abcde.zip"
root = Path(zf)
recurse_print(root) Expected result: abcde.zip/a.txt content of a Actual result: abcde.zip/a.txt content of a Here, Path._add_implied_dirs doesn't add "b/" at all, because there are no direct childs of "b". |
I have attempted a fix to Path._add_implied_dirs and have the changes in my github branch https://github.com/shireenrao/cpython/tree/zipfile. Is it ok if I submitted a PR on this? I have run test_zipfile.py and the test case provided by Jörn Heissler successfully. |
Please do. |
I just submitted my PR. |
Thanks very much shireenrao for the PR, which is now merged with Python 3.8+. I've additionally backported the fix to zipp in 0.6.0. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: