From the course: Python: Working with Files

Unlock this course with a free trial

Join today to access over 25,300 courses taught by industry experts.

Open and read TAR archives

Open and read TAR archives - Python Tutorial

From the course: Python: Working with Files

Open and read TAR archives

- ]Instructor] TAR is another common archive format that supports gzip, bzip2 and lzma compression. We can use the TAR file module to work with TAR files in Python. Let's read a TAR file with Python. TAR file objects followed the same pattern as other file like objects. We can open them in a specific mode using a with statement. If your TAR file happens to be compressed, you'll also need to pass in the compression mode. This mode would be GZ for gzip compression, BZ2 for bzip2 compression and XZ for lzma compression. The file we're working with has gzip compression. So we'll use R:GZ mode. With the TAR file open and read mode, we can start to retrieve information about the archive. For example, we can retrieve all the file names with the get names function. Will access the TAR.getnames. We'll print these to the console. We'll also clean up our comment a bit. Now we can also get information about a specific file using…

Contents