In this article, we will learn how to create or unpack archives in common formats (e.g., .tar, .tgz, or .zip) using shutil module.
The shutil module has two functions —
unpack_archive() — that can exactly be the solution.
Code #1 :
The second argument to
make_archive() is the desired output format. To get a list of supported archive formats, use
Code #2 :
[('bztar', "bzip2'ed tar-file"), ('gztar', "gzip'ed tar-file"), ('tar', 'uncompressed tar file'), ('zip', 'ZIP file')]
Python has other library modules for dealing with the low-level details of various archive formats (e.g., tarfile, zipfile, gzip, bz2, etc.). However, to make or extract an archive, there’s really no need to go so low level.
One can just use these high-level functions in shutil instead. The functions have a variety of additional options for logging, dry-runs, file permissions, and so forth.
Let’s write a script that involves finding files, like a file renaming script or a log archiver utility, but rather not have to call shell utilities from within the Python script, or to provide specialized behavior not easily available by “shelling out.”
To search for files, use the os.walk() function, supplying it with the top-level directory.
Code #3 : Function that finds a specific filename and prints out the full path of all matches.
Save this script as
abc.py and run it from the command line, feeding in the starting point and the name as positional arguments as –
How it works ?
os.walk()method traverses the directory hierarchy for us, and for each directory it enters, it returns a 3-tuple, containing the relative path to the directory it’s inspecting, a list containing all of the directory names in that directory, and a list of filenames in that directory.
- For each tuple, simply check if the target filename is in the files list. If it is,
os.path.join()is used to put together a path.
- To avoid the possibility of weird looking paths like
././foo//bar, two additional functions are used to fix the result.
- The first is
os.path.abspath(), which takes a path that might be relative and forms the absolute path.
- The second is
os.path.normpath(), which will normalize the path, thereby resolving issues with double slashes, multiple references to the current directory, and so on.
Although the code is pretty simple compared to the features of the find utility found on UNIX platforms, it has the benefit of being cross-platform. Furthermore, a lot of additional functionality can be added in a portable manner without much more work.
Code #4 : Function that prints out all of the files that have a recent modification time
It wouldn’t take long for you to build far more complex operations on top of this little function using various features of the os, os.path, glob, and similar modules.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.