I have a 'tafile' which contains files with complete path '/home/usr/path/to/file'. When I extract the file to the curent folder it creates the complete path recursively. Is there a way that I can extract the file with only the base name.
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
Use
TarFile.extractfile()
and write it into a file of your choice.You can change the
arcnames
by hacking theTarInfo
objects you get fromTarfile.getmembers()
. Then you can useTarfile.extractall
to write the members to your chosen destination under their new names.E.g., the following function will select members from an arbitrary subtree of the archive and extract them to a destination under their base names:
Suppose
tar
is aTarFile
instance representing an archive with some members in autilities/misc
directory, and you would like to fold those members into thelocal/bin
directory. You could do:Note the trailing
/
on the directory prefix. We don't want to add themisc
directory to `local/bin', rather, just its members.You can use the function
extractall
to fit your needs. According to the documentation : Extract all members from the archive to the current working directory or directory path.