This chapter will discuss a few additional file operations you can perform using the Python programming language.
How to Zip and Unzip files in Python?
Daily, we rely on specific software like Winrar or 7Zip to zip the files or unzip the files on our computers. When we zip the files, the file’s contents are compressed, so the size gets lessened. Besides that, it also changes the format into an unreadable one.
The Zipping, in general, is done through an algorithm. However, in Python, a module called zipfile has ZipFile class, which can be helpful in both zipping and unzipping the files.
Let us look at a program to understand how file zipping can be done in Python.
#Python program to show the concept of zipping files
#import the zipfile module
from zipfile import *
#create a zipfile
f = ZipFile('FirstZip.zip','w',ZIP_DEFLATED)
#add files for zipping
f.write('fileone.txt')
f.write('filetwo.txt')
f.write('filethree.txt')
#close the zip file
f.close()
print("FirstZip.zip file has been created")
Output:
FirstZip.zip file has been created
In the program above, we imported the zipfile module. Using the ZipFile class object, we created the ‘FirstZip.zip‘ file in write mode. The ZIP_DEFLATED is an attribute available in the ZipFile class object.
After adding the files to the file handler using the write methods for the three files, fileone.txt, filetwo.txt and filethree.txt, we finally closed the file. You can see in the screenshot below that we have successfully created the ZIP file.
Now that we have looked at the zipping operation, let us see how we can unzip the files in the following Python program.
#Python program to unzip the files
#import the zipfile module
from zipfile import *
#opent the zipfile
f = ZipFile('FirstZip.zip','r')
#extract all the files from the given zipfile
f.extractall()
print("Unzipping completed")
Output:
Unzipping completed
In the above program, after importing the zipfile module, we created a file handler for the ‘FirstZip.zip‘ file by opening it in reading mode. Using the extractall() method, we extracted all the files in the zip folder. Before running this program, we deleted the existing files we had created for our first program. And the unzipping program mentioned above brought all those files back from the ‘FirstZip.zip’ file.
How to work with directories in Python?
You can use the ‘os‘ module to work with the directories in Python. ‘os‘ stands for Operating system, and this module works like a charm to perform operations on directories.
Let us look at a program to see how we can use the ‘os‘ module for working with directories in Python.
#python program to use 'os' module to work with directories
#import the os module
import os
#get the current working directory
current_directory = os.getcwd()
print("Current directory: ",current_directory)
#create a directory in this directory
os.mkdir('SampleDirectory')
#list the files in the directory
print("The contents in the directory are: ",os.listdir())
Output:
Current directory: C:\Users\Aatmann\Documents\iSapna\Python\files\Directories
The contents in the directory are: ['osmodule_eg.py', 'SampleDirectory']
- In the program above, we displayed the current working directory using os.getcwd().
- We used ‘os.mkdir‘ to create a directory.
- We used the os.listdir() to list the contents in the current directory. You can notice in the output above that it returned the current directory contents in a list.
There are several additional file operations in Python. However, we think these programs mentioned above will be helpful for you.