mirror of
https://github.com/apachecn/ailearning.git
synced 2026-05-01 22:10:45 +08:00
2020-10-19 21:08:55
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
In [1]:
|
||||
|
||||
```
|
||||
```py
|
||||
import os, shutil, glob
|
||||
import zlib, gzip, bz2, zipfile, tarfile
|
||||
|
||||
@@ -16,7 +16,7 @@ gzip
|
||||
|
||||
In [2]:
|
||||
|
||||
```
|
||||
```py
|
||||
orginal = "this is a test string"
|
||||
|
||||
compressed = zlib.compress(orginal)
|
||||
@@ -26,7 +26,7 @@ print zlib.decompress(compressed)
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
x<EFBFBD>+<EFBFBD><EFBFBD>,V<EFBFBD>D<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>⒢̼tS<EFBFBD><EFBFBD><EFBFBD>
|
||||
this is a test string
|
||||
|
||||
@@ -36,24 +36,24 @@ this is a test string
|
||||
|
||||
In [3]:
|
||||
|
||||
```
|
||||
```py
|
||||
print zlib.adler32(orginal) & 0xffffffff
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
1407780813
|
||||
|
||||
```
|
||||
|
||||
In [4]:
|
||||
|
||||
```
|
||||
```py
|
||||
print zlib.crc32(orginal) & 0xffffffff
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
4236695221
|
||||
|
||||
```
|
||||
@@ -66,7 +66,7 @@ print zlib.crc32(orginal) & 0xffffffff
|
||||
|
||||
In [5]:
|
||||
|
||||
```
|
||||
```py
|
||||
content = "Lots of content here"
|
||||
with gzip.open('file.txt.gz', 'wb') as f:
|
||||
f.write(content)
|
||||
@@ -77,7 +77,7 @@ with gzip.open('file.txt.gz', 'wb') as f:
|
||||
|
||||
In [6]:
|
||||
|
||||
```
|
||||
```py
|
||||
with gzip.open('file.txt.gz', 'rb') as f:
|
||||
file_content = f.read()
|
||||
|
||||
@@ -85,7 +85,7 @@ print file_content
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
Lots of content here
|
||||
|
||||
```
|
||||
@@ -94,7 +94,7 @@ Lots of content here
|
||||
|
||||
In [7]:
|
||||
|
||||
```
|
||||
```py
|
||||
with gzip.open('file.txt.gz', 'rb') as f_in, open('file.txt', 'wb') as f_out:
|
||||
shutil.copyfileobj(f_in, f_out)
|
||||
|
||||
@@ -104,20 +104,20 @@ with gzip.open('file.txt.gz', 'rb') as f_in, open('file.txt', 'wb') as f_out:
|
||||
|
||||
In [8]:
|
||||
|
||||
```
|
||||
```py
|
||||
with open("file.txt") as f:
|
||||
print f.read()
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
Lots of content here
|
||||
|
||||
```
|
||||
|
||||
In [9]:
|
||||
|
||||
```
|
||||
```py
|
||||
os.remove("file.txt.gz")
|
||||
|
||||
```
|
||||
@@ -128,7 +128,7 @@ os.remove("file.txt.gz")
|
||||
|
||||
In [10]:
|
||||
|
||||
```
|
||||
```py
|
||||
orginal = "this is a test string"
|
||||
|
||||
compressed = bz2.compress(orginal)
|
||||
@@ -138,7 +138,7 @@ print bz2.decompress(compressed)
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
BZh91AY&SY*<EFBFBD><EFBFBD>v <EFBFBD><EFBFBD>@"<EFBFBD><EFBFBD> 10"zi<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>FLT`<EFBFBD>軒)„<EFBFBD>P<EFBFBD>˰
|
||||
this is a test string
|
||||
|
||||
@@ -150,7 +150,7 @@ this is a test string
|
||||
|
||||
In [11]:
|
||||
|
||||
```
|
||||
```py
|
||||
for i in range(10):
|
||||
shutil.copy("file.txt", "file.txt." + str(i))
|
||||
|
||||
@@ -160,7 +160,7 @@ for i in range(10):
|
||||
|
||||
In [12]:
|
||||
|
||||
```
|
||||
```py
|
||||
f = zipfile.ZipFile('files.zip','w')
|
||||
|
||||
for name in glob.glob("*.txt.[0-9]"):
|
||||
@@ -175,13 +175,13 @@ f.close()
|
||||
|
||||
In [13]:
|
||||
|
||||
```
|
||||
```py
|
||||
f = zipfile.ZipFile('files.zip','r')
|
||||
print f.namelist()
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
['file.txt.9', 'file.txt.6', 'file.txt.2', 'file.txt.1', 'file.txt.5', 'file.txt.4', 'file.txt.3', 'file.txt.7', 'file.txt.8', 'file.txt.0']
|
||||
|
||||
```
|
||||
@@ -190,7 +190,7 @@ print f.namelist()
|
||||
|
||||
In [14]:
|
||||
|
||||
```
|
||||
```py
|
||||
for name in f.namelist():
|
||||
print name, "content:", f.read(name)
|
||||
|
||||
@@ -198,7 +198,7 @@ f.close()
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
```py
|
||||
file.txt.9 content: Lots of content here
|
||||
file.txt.6 content: Lots of content here
|
||||
file.txt.2 content: Lots of content here
|
||||
@@ -222,7 +222,7 @@ file.txt.0 content: Lots of content here
|
||||
|
||||
In [15]:
|
||||
|
||||
```
|
||||
```py
|
||||
f = tarfile.open("file.txt.tar", "w")
|
||||
f.add("file.txt")
|
||||
f.close()
|
||||
@@ -233,7 +233,7 @@ f.close()
|
||||
|
||||
In [16]:
|
||||
|
||||
```
|
||||
```py
|
||||
os.remove("file.txt")
|
||||
os.remove("file.txt.tar")
|
||||
os.remove("files.zip")
|
||||
|
||||
Reference in New Issue
Block a user