![]() Resources & Coursesįinally, if you're a beginner and want to learn Python, I suggest you take the Python For Everybody Coursera course, in which you'll learn a lot about Python, good luck! In this tutorial, we have explored compression and decompression using tarfile module, you can also use zipfile module to work with ZIP archives, bz2 module for bzip2 compressions, gzip, or zlib modules for gzip files. Downloading archive files and decompressing them.Transferring folders across the network after compressing them.Okay, we are done! You can be creative with this, here are some ideas: This will decompress the previous archive we just compressed to a new folder called extracted: If you want to decompress: decompress("", "extracted") This will compress the test.txt file and folder in the current directory to a new tar archive file called as shown in the following example figure: Note that we can alternatively use the extractall() for that (which is preferred in the official documentation). After that, I made an optional parameter 'member' in case we want to extract specific files (not all archives), if 'members' isn't specified, we gonna get all files in the archive using the getmembers() method which returns all the members of the archive as a Python list.Īnd then for each member, extract it using the extract() method which extracts a member from the archive to the 'path' directory we specified. # tar.extractall(members=members, path=path)įirst, we opened the archive file as reading with gzip compression. # set the progress description of the progress bar # add file/folder/link to the tar file (compress) Tar = tarfile.open(tar_file, mode="w:gz") Let's start by compression, the following function is responsible for compressing a file/folder or a list of files/folders: def compress(tar_file, members):Īdds files (`members`) to a tar_file and compress it Open up a new Python file and: import tarfileįrom tqdm import tqdm # pip3 install tqdm Let's get started, we will be using the tarfile built-in module, so we don't have to install anything, you can optionally install tqdm just for printing progress bars: pip3 install tqdm Related: How to Encrypt and Decrypt Files in Python. You may also want to add a compression/decompression feature in your application, or you have thousands of compressed files and you want to decompress them in one click, this tutorial can help. For example, when downloading machine learning datasets in which you want a piece of code to download, extract and load them into memory automatically. You may ask, why would I learn to compress files in Python where there are already provided tools out there? Well, de compressing files programmatically without any manual clicks is extremely useful. However, in this tutorial, you will learn how to compress and decompress files using the Python programming language. Compressing files in modern operating systems is usually pretty simple. Disclosure: This post may contain affiliate links, meaning when you click the links and make a purchase, we receive a commission.Ī compressed file is a sort of archive that contains one or more files that have been reduced in size.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |