Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite7538ee

Browse files
committed
feat: zipfile module
1 parent6178fb6 commite7538ee

File tree

4 files changed

+107
-54
lines changed

4 files changed

+107
-54
lines changed

‎.vscode/settings.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
"IGNORECASE",
6161
"imag",
6262
"importlib",
63+
"infolist",
6364
"isabs",
6465
"isalpha",
6566
"isdecimal",

‎docs/cheatsheet/reading-and-writing-files.md‎

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -110,57 +110,3 @@ Just like dictionaries, `shelf` values have `keys()` and `values()` methods that
110110
# ['cats']
111111
# [['Zophie', 'Pooka', 'Simon']]
112112
```
113-
114-
##Reading ZIP files
115-
116-
```python
117-
>>>import zipfile, os
118-
119-
>>> os.chdir('C:\\')# move to the folder with example.zip
120-
>>>with zipfile.ZipFile('example.zip')as example_zip:
121-
...print(example_zip.namelist())
122-
... spam_info= example_zip.getinfo('spam.txt')
123-
...print(spam_info.file_size)
124-
...print(spam_info.compress_size)
125-
...print('Compressed file is%sx smaller!'% (round(spam_info.file_size/ spam_info.compress_size,2)))
126-
...
127-
# ['spam.txt', 'cats/', 'cats/catnames.txt', 'cats/zophie.jpg']
128-
# 13908
129-
# 3828
130-
# 'Compressed file is 3.63x smaller!'
131-
```
132-
133-
##Extracting from ZIP Files
134-
135-
The`extractall()` method for ZipFile objects extracts all the files and folders from a ZIP file into the current working directory.
136-
137-
```python
138-
>>>import zipfile, os
139-
140-
>>> os.chdir('C:\\')# move to the folder with example.zip
141-
142-
>>>with zipfile.ZipFile('example.zip')as example_zip:
143-
... example_zip.extractall()
144-
```
145-
146-
The`extract()` method for ZipFile objects will extract a single file from the ZIP file:
147-
148-
```python
149-
>>>with zipfile.ZipFile('example.zip')as example_zip:
150-
...print(example_zip.extract('spam.txt'))
151-
...print(example_zip.extract('spam.txt','C:\\some\\new\\folders'))
152-
...
153-
# 'C:\\spam.txt'
154-
# 'C:\\some\\new\\folders\\spam.txt'
155-
```
156-
157-
##Creating and Adding to ZIP Files
158-
159-
```python
160-
>>>import zipfile
161-
162-
>>>with zipfile.ZipFile('new.zip','w')as new_zip:
163-
... new_zip.write('spam.txt',compress_type=zipfile.ZIP_DEFLATED)
164-
```
165-
166-
This code will create a new ZIP file named new.zip that has the compressed contents of spam.txt.

‎docs/modules/zipfile-module.md‎

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
---
2+
title:Python Zipfile Module - Python Cheatsheet
3+
description:The random module is a built-in module that allow us to generate random elements.
4+
---
5+
6+
#Python Zipfile Module
7+
8+
<base-disclaimer>
9+
<base-disclaimer-title>
10+
From the <a target="_blank" href="https://docs.python.org/3/library/zipfile.html">Python 3 documentation</a>
11+
</base-disclaimer-title>
12+
<base-disclaimer-content>
13+
This module provides tools to create, read, write, append, and list a ZIP file.
14+
</base-disclaimer-content>
15+
</base-disclaimer>
16+
17+
##Reading ZIP files
18+
19+
```python
20+
>>>with zipfile.ZipFile('example.zip')as example_zip:
21+
...print(example_zip.namelist())
22+
... spam_info= example_zip.getinfo('spam.txt')
23+
...print(spam_info.file_size)
24+
...print(spam_info.compress_size)
25+
...print('Compressed file is%sx smaller!'% (round(spam_info.file_size/ spam_info.compress_size,2)))
26+
...
27+
# ['spam.txt', 'cats/', 'cats/catnames.txt', 'cats/zophie.jpg']
28+
# 13908
29+
# 3828
30+
# 'Compressed file is 3.63x smaller!'
31+
```
32+
33+
##Extracting from ZIP Files
34+
35+
The`extractall()` method for ZipFile objects extracts all the files and folders from a ZIP file into the current working directory.
36+
37+
```python
38+
>>>with zipfile.ZipFile('example.zip')as example_zip:
39+
... example_zip.extractall()
40+
```
41+
42+
The`extract()` method for ZipFile objects will extract a single file from the ZIP file:
43+
44+
```python
45+
>>>with zipfile.ZipFile('example.zip')as example_zip:
46+
...print(example_zip.extract('spam.txt'))
47+
...print(example_zip.extract('spam.txt','C:\\some\\new\\folders'))
48+
...
49+
# 'C:\\spam.txt'
50+
# 'C:\\some\\new\\folders\\spam.txt'
51+
```
52+
53+
##Creating and Adding to ZIP Files
54+
55+
```python
56+
>>>import zipfile
57+
>>>
58+
>>>with zipfile.ZipFile('new.zip','w')as new_zip:
59+
... new_zip.write('spam.txt',compress_type=zipfile.ZIP_DEFLATED)
60+
```
61+
62+
This code will create a new ZIP file named new.zip that has the compressed contents of spam.txt.
63+
64+
##Reading metadata of ZIP files
65+
66+
###Getting the filenames with namelist()
67+
68+
```python
69+
>>>import zipfile
70+
>>>
71+
>>>with zipfile.ZipFile('example.zip','r')as zf:
72+
...print(zf.namelist())
73+
...
74+
# ['README.txt']
75+
```
76+
77+
###Getting all metadata with infolist()
78+
79+
```python
80+
>>>import datetime
81+
>>>import zipfile
82+
>>>
83+
>>>with zipfile.ZipFile(archive_name)as zf:
84+
...for infoin zf.infolist():
85+
... system='Windows'if info.create_system==0else'Unix'
86+
... modified= datetime.datetime(*info.date_time)
87+
...print(info.filename)
88+
...print(f'Comment :{info.comment}')
89+
...print(f'Modified :{modified}')
90+
...print(f'System :{system}')
91+
...print(f'ZIP version :{info.create_version}')
92+
...print(f'Compressed :{info.compress_size} bytes')
93+
...print(f'Uncompressed:{info.file_size} bytes')
94+
...
95+
# README.txt
96+
# Comment : b''
97+
# Modified : 2022-11-15 06:48:02
98+
# System : Unix
99+
# ZIP version : 30
100+
# Compressed : 65 bytes
101+
# Uncompressed: 76 bytes
102+
```

‎src/store/navigation.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ export const useNavigationStore = defineStore('navigation', {
136136
name:'Pathlib',
137137
path:'/modules/pathlib-module',
138138
},
139+
{
140+
name:'Zipfile',
141+
path:'/modules/zipfile-module',
142+
},
139143
],
140144
}),
141145
})

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp