![]() ![]() 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. 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. When you execute the code you can see the file is created on the right side of the panel with name “guru99.zip” Using the write function on the “newzip” variable, we add the files “guru99.txt” and “” to the archive.We used variable “newzip” to refer to the zip file we created.Creating a new Zipfile class, requires to pass in permission because it’s a file, so you need to write information into the file as newzip.We create a new Zipfile with name ( “testguru99.zip, “w”).This module gives full control over creating zip files Import Zipfile class from zip file Python module.In our case, we will include two files under archive “guru99.txt” and “”. Step 4) In Python we can have more control over archive since we can define which specific file to include under archive. Step 3) When you double-click on the file, you will see the list all the files in there. Now your archive.zip file will appear on your O.S (Windows Explorer) 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 When you run the code, you can see the archive zip file is created on the right side of the panel.So everything in the directory will be zipped After then we pass in the root directory of things we want to be zipped up.Then we call the module “shutil.make_archive(“guru99 archive, “zip”, root_dir)” to create archive file, which will be in zip format.Use the split function to split out the directory and the file name from the path to the location of the text file (guru99).Import make_archive class from module shutil.Items from x and y are paired according to their respective indexes.One good way to understand what's going on is to print at each step: x = (x, y) = tuple(zip(*zip(x,y))) is true if and only if the two following statements are true: If someone isn't familiar with how that works in Python, it makes it easier to get lost as to what's actually happening. In the second call, it's leveraging Python's ability to automatically unpack a list (or collection?) return value into multiple variable references, each reference being the individual tuple. That is, when zip is called the first time, the return value is assigned to a single variable, thereby creating a list reference (containing the created tuple list). What gave me problems with understanding the zip example was the asymmetry in the handling of the zip call return value(s). I'm extremely new to Python so this just recently tripped me up, but it had to do more with how the example was presented and what was emphasized.
0 Comments
Leave a Reply. |