how to upload a zipped library to iphython notebook and upzip it
This article explains how one can perform various operations on a zip file using a unproblematic python programme.
What is a zip file?
ZIP is an archive file format that supports lossless data pinch. By lossless compression, nosotros mean that the compression algorithm allows the original data to be perfectly reconstructed from the compressed data. And then, a ZIP file is a single file containing i or more compressed files, offer an ideal way to brand large files smaller and keep related files together.
Why practise we demand nil files?
- To reduce storage requirements.
- To improve transfer speed over standard connections.
To work on zip files using python, we volition use an inbuilt python module called zipfile.
i. Extracting a aught file
from zipfile import ZipFile
file_name = "my_python_files.nada"
with ZipFile(file_name, 'r' ) equally cipher :
zip .printdir()
print ( 'Extracting all the files at present...' )
zip .extractall()
print ( 'Done!' )
The above program extracts a zip file named "my_python_files.zip" in the aforementioned directory every bit of this python script.
The output of above program may look like this:
Let us try to understand the to a higher place code in pieces:
-
from zipfile import ZipFile
ZipFile is a course of zipfile module for reading and writing zip files. Here we import only class ZipFile from zipfile module.
-
with ZipFile(file_name, 'r') as zippo:
Here, a ZipFile object is fabricated by calling ZipFile constructor which accepts zip file name and mode parameters. We create a ZipFile object in READ mode and proper name it as zip.
-
nothing.printdir()
printdir() method prints a table of contents for the archive.
-
zip.extractall()
extractall() method volition extract all the contents of the zero file to the electric current working directory. You can as well call extract() method to excerpt any file by specifying its path in the zip file.
For case:zip.extract('python_files/python_wiki.txt')This volition extract just the specified file.
If you desire to read some specific file, you can go like this:
data = zip.read(name_of_file_to_read)
ii. Writing to a aught file
Consider a directory (folder) with such a format:
Here, we will need to crawl the whole directory and its sub-directories in order to go a list of all file paths earlier writing them to a zippo file.
The following plan does this by crawling the directory to be zipped:
from zipfile import ZipFile
import bone
def get_all_file_paths(directory):
file_paths = []
for root, directories, files in os.walk(directory):
for filename in files:
filepath = os.path.join(root, filename)
file_paths.append(filepath)
return file_paths
def main():
directory = './python_files'
file_paths = get_all_file_paths(directory)
impress ( 'Following files will be zipped:' )
for file_name in file_paths:
print (file_name)
with ZipFile( 'my_python_files.nix' , 'w' ) as zip :
for file in file_paths:
cypher .write( file )
print ( 'All files zipped successfully!' )
if __name__ = = "__main__" :
primary()
The output of above program looks like this:
Permit us endeavor to empathize to a higher place lawmaking by dividing into fragments:
-
def get_all_file_paths(directory): file_paths = [] for root, directories, files in os.walk(directory): for filename in files: filepath = os.path.join(root, filename) file_paths.append(filepath) return file_paths
First of all, to become all file paths in our directory, we accept created this role which uses the bone.walk() method. In each iteration, all files present in that directory are appended to a list chosen file_paths.
In the end, we render all the file paths. -
file_paths = get_all_file_paths(directory)
Hither we laissez passer the directory to be zipped to the get_all_file_paths() function and obtain a list containing all file paths.
-
with ZipFile('my_python_files.nil','due west') as goose egg:Hither, we create a ZipFile object in WRITE manner this time.
-
for file in file_paths: zip.write(file)
Here, we write all the files to the zip file one by one using write method.
3. Getting all information nearly a nada file
from zipfile import ZipFile
import datetime
file_name = "instance.zip"
with ZipFile(file_name, 'r' ) every bit goose egg :
for info in zero .infolist():
print (info.filename)
print ( '\tModified:\t' + str (datetime.datetime( * info.date_time)))
print ( '\tSystem:\t\t' + str (info.create_system) + '(0 = Windows, 3 = Unix)' )
print ( '\tZIP version:\t' + str (info.create_version))
print ( '\tCompressed:\t' + str (info.compress_size) + ' bytes' )
print ( '\tUncompressed:\t' + str (info.file_size) + ' bytes' )
The output of in a higher place program may look like this:
for info in zip.infolist():
Here, infolist() method creates an instance of ZipInfo class which contains all the information about the zip file.
We tin admission all information like concluding modification engagement of files, file names, arrangement on which files were created, Zip version, size of files in compressed and uncompressed form, etc.
This article is contributed past Nikhil Kumar. If you similar GeeksforGeeks and would like to contribute, you lot can also write an commodity using write.geeksforgeeks.org or postal service your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks primary page and assistance other Geeks.
Please write comments if y'all observe anything incorrect, or you want to share more than data most the topic discussed above.
Source: https://www.geeksforgeeks.org/working-zip-files-python/
Post a Comment for "how to upload a zipped library to iphython notebook and upzip it"