PY0020 Python ZIP file with Example
Posted by Superadmin on November 10 2018 18:07:58

Python allows you to quickly create zip/tar archives.

Following command will zip entire directory

shutil.make_archive(output_filename, 'zip', dir_name)

Following command gives you control on the files you want to archive

ZipFile.write(filename)

Here are the steps to create Zip File in Python

Step 1) To create an archive file from Python, make sure you have your import statement correct and in order. Here the import statement for the archive is from shutil import make_archive

Python OS Module, Shell Script Commands

Code Explanation

Step 2)

  • Once your archive file is made, you can right-click on the file and select the O.S, and it will show your archive files in it as shown below

    Python OS Module, Shell Script Commands

    Now your archive.zip file will appear on your O.S (Windows Explorer)

    Python OS Module, Shell Script Commands

    Step 3) When you double-click on the file, you will see the list all the files in there.

    Python OS Module, Shell Script Commands

    Step 4) In Python we can have more control over archive since we can define which specific file to include under archive. In our case, we will include two files under archive "guru99.txt" and "guru99.txt.bak".

    Python OS Module, Shell Script Commands

    Code Explanation

    When you execute the code you can see the file is created on the right side of the panel with name "guru99.zip"

    Note: Here we don't give any command to "close" the file like "newzip.close" because we use "With" scope lock, so when program falls outside of this scope the file will be cleaned up and is closed automatically.

    Step 5) When you -> right click on file (testguru99.zip) and -> select your O.S (Windows Explorer), it will show the archive files in the folder as shown below.

    Python OS Module, Shell Script Commands

    When you double click on file "testguru99.zip", it will open another window, and this will show the files included in it.

    Python OS Module, Shell Script Commands

    Here is the complete code

    Python 2 Example

    import os
    import shutil
    from zipfile import ZipFile
    from os import path
    from shutil import make_archive
    
    def main():
    # Check if file exists
    	if path.exists("guru99.txt"):
    # get the path to the file in the current directory
    	src = path.realpath("guru99.txt");
    # rename the original file
    	os.rename("career.guru99.txt","guru99.txt")
    # now put things into a ZIP archive
    	root_dir,tail = path.split(src)
        shutil.make_archive("guru99 archive", "zip", root_dir)
    # more fine-grained control over ZIP files
    	with ZipFile("testguru99.zip","w") as newzip:
    	newzip.write("guru99.txt")
    	    newzip.write("guru99.txt.bak")
    if __name__== "__main__":
    	  main()

    Python 3 Example

    import os
    import shutil
    from zipfile import ZipFile
    from os import path
    from shutil import make_archive
    
        # Check if file exists
           if path.exists("guru99.txt"):
        # get the path to the file in the current directory
            src = path.realpath("guru99.txt");
        # rename the original file
            os.rename("career.guru99.txt","guru99.txt")
        # now put things into a ZIP archive
            root_dir,tail = path.split(src)
            shutil.make_archive("guru99 archive","zip",root_dir)
        # more fine-grained control over ZIP files
            with ZipFile("testguru99.zip", "w") as newzip:
                newzip.write("guru99.txt")
                newzip.write("guru99.txt.bak")

    Summary