Skip to content

Commit 48cef73

Browse files
committed
ch08
1 parent 32c12d8 commit 48cef73

34 files changed

+796
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is content1.txt
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is content2.txt
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is subfolder/content3.txt
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is subfolder/content4.txt

ch08/files/compression/tar.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# files/compression/tar.py
2+
import tarfile
3+
4+
with tarfile.open('example.tar.gz', 'w:gz') as tar:
5+
tar.add('content1.txt')
6+
tar.add('content2.txt')
7+
tar.add('subfolder/content3.txt')
8+
tar.add('subfolder/content4.txt')
9+
10+
with tarfile.open('example.tar.gz', 'r:gz') as tar:
11+
tar.extractall('extract_tar')

ch08/files/compression/zip.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# files/compression/zip.py
2+
from zipfile import ZipFile
3+
4+
5+
with ZipFile('example.zip', 'w') as zp:
6+
zp.write('content1.txt')
7+
zp.write('content2.txt')
8+
zp.write('subfolder/content3.txt')
9+
zp.write('subfolder/content4.txt')
10+
11+
12+
with ZipFile('example.zip') as zp:
13+
zp.extract('content1.txt', 'extract_zip')
14+
zp.extract('subfolder/content3.txt', 'extract_zip')

ch08/files/existence.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# files/existence.py
2+
from pathlib import Path
3+
4+
p = Path('fear.txt')
5+
path = p.parent.absolute()
6+
7+
print(p.is_file()) # True
8+
print(path) # /Users/fab/srv/lpp3e/ch08/files
9+
print(path.is_dir()) # True
10+
11+
q = Path('/Users/fab/srv/lpp3e/ch08/files')
12+
print(q.is_dir()) # True

ch08/files/fear.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
An excerpt from Fear - By Thich Nhat Hanh
2+
3+
The Present Is Free from Fear
4+
5+
When we are not fully present, we are not really living. We’re not really there, either for our loved ones or for ourselves. If we’re not there, then where are we? We are running, running, running, even during our sleep. We run because we’re trying to escape from our fear.
6+
7+
We cannot enjoy life if we spend our time and energy worrying about what happened yesterday and what will happen tomorrow. If we’re afraid all the time, we miss out on the wonderful fact that we’re alive and can be happy right now. In everyday life, we tend to believe that happiness is only possible in the future. We’re always looking for the “right” conditions that we don’t yet have to make us happy. We ignore what is happening right in front of us. We look for something that will make us feel more solid, more safe, more secure. But we’re afraid all the time of what the future will bring—afraid we’ll lose our jobs, our possessions, the people around us whom we love. So we wait and hope for that magical moment—always sometime in the future—when everything will be as we want it to be. We forget that life is available only in the present moment. The Buddha said, “It is possible to live happily in the present moment. It is the only moment we have.”

ch08/files/listing.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# files/listing.py
2+
from pathlib import Path
3+
4+
5+
p = Path('.')
6+
7+
# use pattern "*.*" to exclude directories
8+
for entry in p.glob('*'):
9+
print('File:' if entry.is_file() else 'Folder:', entry)
10+
11+
12+
"""
13+
File: fixed_amount.py
14+
File: existence.py
15+
File: manipulation.py
16+
File: print_file.py
17+
File: read_write_bin.py
18+
File: paths.py
19+
File: open_with.py
20+
File: walking.py
21+
File: tmp.py
22+
File: read_write.py
23+
File: listing.py
24+
File: write_not_exists.py
25+
File: buffer.py
26+
Folder: compression
27+
File: ops_create.py
28+
File: fear.txt
29+
File: open_try.py
30+
"""

ch08/files/manipulation.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# files/manipulation.py
2+
from collections import Counter
3+
from string import ascii_letters
4+
5+
6+
chars = ascii_letters + ' '
7+
8+
9+
def sanitize(s, chars):
10+
return ''.join(c for c in s if c in chars)
11+
12+
13+
def reverse(s):
14+
return s[::-1]
15+
16+
17+
with open('fear.txt') as stream:
18+
lines = [line.rstrip() for line in stream]
19+
20+
21+
# let's write the mirrored version of the file
22+
with open('raef.txt', 'w') as stream:
23+
stream.write('\n'.join(reverse(line) for line in lines))
24+
25+
26+
# now we can calculate some statistics
27+
lines = [sanitize(line, chars) for line in lines]
28+
whole = ' '.join(lines)
29+
30+
31+
# we perform comparisons on the lowercased version of `whole`
32+
cnt = Counter(whole.lower().split())
33+
34+
# we can print the N most common words
35+
print(cnt.most_common(3))

0 commit comments

Comments
 (0)