![]() ![]() # 'close()' method is used to close the file # You must close the file if you don't open a file using 'with' keyword # 'read()' method of the file prints all the content of the file. Text_file = file.open(name = file.namelist(), mode = 'r') # 'pwd' is optional -> if it has password mention otherwise leave it ![]() # ZipFile.open(path = filepath, mode = mode_type, pwd = password) opens the members of an archive file # It raises a KeyError if it doesn't contain the mentioned file # ZipFile.getinfo(path = filepath) returns the information about a member of Zip file. # ZipFile.namelist() returns a list containing all the members with names of an archive file # list() returns a list containing all the members of an archive file Let's see some methods of the ZipFile class. Zipfile.ZipFile contains many methods like extract, open getinfo, setpassword, etc., to work the Zip files. You've already used the zipfile.ZipFile class to read Zip files in previous examples. ![]() Print(dir(zipfile.ZipFile)) # accessing the 'ZipFile' class Now, explore the methods of the ZipFile class using the dir() objects. ![]() It has some methods which are used to handle the Zip files. Zipfile.ZipFile is used to write and read the Zip files. The most common class which is used to work with Zip Files is ZipFile class. Let's see some classes of the zipfile module. You use the class methods and attributes wherever you want by creating the instances of class. In simple words, class is a set of methods and attributes. Print('Error: File size if too large') # if the file size is too large to open it prints the error you have writtenĬhoose a Zip file which best suits for the Exception handling and then tries to run the program. With zipfile.ZipFile('sample_file.zip', mode = 'r', allowZip64 = True) as file: # here enabling the 'Zip64' With zipfile.ZipFile('sample_file.zip') as file:Įxcept zipfile.LargeZipFile: # it raises an 'LargeZipFile' error because you didn't enable the 'Zip64' If you don't enable it, LargeZipFile will raise. Suppose if you want to work with a large Zip file, you need to enable the ZIP64 functionality while opening the Zip. With zipfile.ZipFile('sample_file.zip') as file: # opening the zip file using 'zipfile.ZipFile' classĮxcept zipfile.BadZipFile: # if the zip file has any errors then it prints the error message which you wrote under the 'except' block Zipfile.BadZipFile is an exception in the zipfile module. Let's see all exceptions in zipfile module. If you are not familiar with the error handling, go to Pythons Error Handling documentation to learn the error handling. In Python, you use try, except, finally keywords for the error handling. ExceptionsĮxception is a message which is used to display the exact error as you like. Let's see some useful Exceptions, Classes, and Methods with brief explanations. You will learn only some classes and methods to work with the Zip files. But, you are not going to learn all of them. You have seen a bunch of classes and methods right. import zipfile # importing the 'zipfile' module See the code to get all the classes and methods of the zipfile module. Zip files transfer faster than the individual file over many connections.Įxplore all the methods and classes of the zipfile module using dir() method.Zip files help to reduce the data size.Zip files help you to put all related files in one place.The Zip file is a single file containing one or more compressed files. Zip is an archive file format which supports the lossless data compression. Open this link to download all of the Zip folders which I have used in the upcoming sections. If you don't know Python take DataCamp's free Intro to Python for Data Science course to learn Python language or read Pythons official documentation.Python concepts like conditionals, loops, functions, classes, etc.,.If you don't know the file handling, head over to the W3Schools File Handling section to learn. You must know the file handling of Python to understand Zip file handling. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |