You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Python3.9+ throws KeyError when extracting folder with trailing slash #127636
Calling extract() on a member of a tar file that is a directory and contains a trailing forward slash in its name in Python3.9+ causes a KeyError, since the member cannot be found due to what appears to be the trimmed slash here:
Traceback (most recent call last):
File "C:\temp\tar_test\test.py", line 23, in <module>
extract_tar(archive, dest)
File "C:\temp\tar_test\test.py", line 15, in extract_tar
tar.extract(item, dest)
File "c:\python39\lib\tarfile.py", line 2077, in extract
tarinfo = self.getmember(member)
File "c:\python39\lib\tarfile.py", line 1799, in getmember
raise KeyError("filename %r not found" % name)
KeyError: "filename './mydir/' not found"
I've just verified that this indeed happens in both Python 3.12.8 as well as in 3.13.1.
I must admit, that in my sample code above, I am not setting the TarInfo member type to tarfile.DIRTYPE. When I do set it, the trailing / is omitted from the name in the member when the sample Python code (above) adds it to the tar file.
However, in my production code, I do end up having to work with a tar file which has a TarInfo member with the type tarfile.DIRTYPE and it does have a trailing / in its name. That tar file is not created by Python's tarfile module; my understanding is that it's being created by the tar cli itself. Still trying to figure out how that is happening, but the error presented by my sample code above is the same, caused by the same line of code that I pointed to. Indeed, if I make a modification to remove the rstrip on that line of code, my folder is extracted properly without error since the name is found.
Also, if instead of passing in the member's name to the tar.extract() function, I instead pass the TarInfo object itself, that folder with the trailing / is extracted properly, so I can work around it that way.
Unfortunately, I can't supply my "production" archive, since it contains product code names that can't be made public.
Activity
ericvsmith commentedon Dec 5, 2024
Have you verified this happens on bugfix supported versions of Python (3.12, 3.13)?
ovianao commentedon Dec 6, 2024
I've just verified that this indeed happens in both Python 3.12.8 as well as in 3.13.1.
I must admit, that in my sample code above, I am not setting the TarInfo member type to tarfile.DIRTYPE. When I do set it, the trailing / is omitted from the name in the member when the sample Python code (above) adds it to the tar file.
However, in my production code, I do end up having to work with a tar file which has a TarInfo member with the type tarfile.DIRTYPE and it does have a trailing / in its name. That tar file is not created by Python's tarfile module; my understanding is that it's being created by the tar cli itself. Still trying to figure out how that is happening, but the error presented by my sample code above is the same, caused by the same line of code that I pointed to. Indeed, if I make a modification to remove the rstrip on that line of code, my folder is extracted properly without error since the name is found.
Also, if instead of passing in the member's name to the tar.extract() function, I instead pass the TarInfo object itself, that folder with the trailing / is extracted properly, so I can work around it that way.
Unfortunately, I can't supply my "production" archive, since it contains product code names that can't be made public.
yuanx749 commentedon Aug 12, 2026
Friendly ping for review when someone has time. Thanks!
pythongh-127636: Clarify the trailing-slash tarfile test
gpshead commentedon Sep 8, 2026
thanks for the fix, reviewed and set to automerge and backport.
gh-127636: Fix tarfile extracting trailing slash member names (GH-152984
pythongh-127636: Fix tarfile extracting trailing slash member names (p…
pythongh-127636: Fix tarfile extracting trailing slash member names (p…