Skip to content

Commit 54a3439

Browse files
committed
reading zip files
1 parent 80f9c04 commit 54a3439

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

Modules/OrganizingFiles/lesson_plan.txt

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,23 @@ for folderName, subfolders, filenames in os.walk(os.path.abspath('.')):
7474
print('')
7575
```
7676

77-
### Compressing Files with the zipfile Module
78-
A zipfile is an archive file that allows the storage of multiple files and
77+
## Compressing Files with the zipfile Module
78+
A ZIP file is an archive file that allows the storage of multiple files and
7979
folders into one.
80+
81+
### Reading ZIP Files
82+
To read a ZIP file, we must create a ZipFile object first. This is similar to
83+
the File object created for file input/output. To create a ZipFile object, call
84+
zipfile.ZipFile('name.zip'). zipfile is the name of the Python module. Create
85+
a zipfile called "example.zip" to test this program and try it out with the
86+
Python interactive shell
87+
```python
88+
import zipfile, os
89+
exampleZip = zipfile.ZipFile('example.zip')
90+
exampleZip.namelist()
91+
fileInfo = exampleZip.getinfo('some_file.txt')
92+
fileInfo.file_size
93+
fileInfo.compress_size
94+
print('Compressed file is %sx smaller!' % (round(fileInfo.file_size / fileInfo.compress_size, 2)))
95+
exampleZip.close()
96+
```

0 commit comments

Comments
 (0)