Skip to content

Commit bdf9378

Browse files
committed
Finished chapter 8
1 parent 2c700e8 commit bdf9378

16 files changed

Lines changed: 174 additions & 0 deletions

08-file/bacon.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Hello world!
2+
Bacon is not a vegetable.

08-file/baconFile.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import os
2+
3+
baconFile = open('bacon.txt', 'w')
4+
baconFile.write('Hello world!\n')
5+
baconFile.close()
6+
7+
baconFile = open('bacon.txt', 'a')
8+
baconFile.write('Bacon is not a vegetable.')
9+
baconFile.close()
10+
11+
baconFile = open('bacon.txt')
12+
content = baconFile.read()
13+
baconFile.close()
14+
15+
print(content)

08-file/find.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import os
2+
import re
3+
import sys
4+
5+
6+
def getTxtFile(directory):
7+
txts = []
8+
txtSuffix = r'\.txt$'
9+
txtRegex = re.compile(txtSuffix)
10+
for filename in os.listdir(directory):
11+
mo = txtRegex.search(filename)
12+
if mo:
13+
txts.append(filename)
14+
return txts
15+
16+
def find(query, directory):
17+
txts = getTxtFile(directory)
18+
print(txts)
19+
queryRegex = re.compile(query)
20+
for txt in txts:
21+
with open(txt) as f:
22+
for line in f:
23+
mo = queryRegex.search(line)
24+
if mo:
25+
print(txt.ljust(20) + ": " + line, end='')
26+
27+
find("world", ".")

08-file/madlibs.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import re
2+
3+
text = "The ADJECTIVE panda walked to the NOUN and then VERB. A nearby NOUN was unaffected by these events."
4+
5+
regex = '[A-Z]{2,}'
6+
wordsRegex = re.compile(regex)
7+
8+
while True:
9+
mo = wordsRegex.search(text)
10+
if mo == None:
11+
#print('No words to be replaced.')
12+
break
13+
word = input('Enter an ' + mo.group().lower() + ':\n')
14+
text = wordsRegex.sub(' '+word, text, count=1)
15+
16+
print(text)
17+

08-file/mcb.bak

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
'b', (512, 17)

08-file/mcb.dat

529 Bytes
Binary file not shown.

08-file/mcb.dir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
'b', (512, 17)

08-file/mcb.pyw

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#! python3
2+
# mcb.pyw - Saves and loads pieces of text to the clipboard.
3+
# Usage: py.exe mcb.pyw save <keyword> - Saves clipboard to keyword.
4+
# py.exe mcb.pyw <keyword> - Loads keyword to clipboard.
5+
# py.exe mcb.pyw list - Loads all keywords to clipboard.
6+
7+
import shelve, pyperclip, sys
8+
9+
mcbShelf = shelve.open('mcb')
10+
11+
# Save clipboard content.
12+
if len(sys.argv) == 3:
13+
if sys.argv[1].lower() == 'save':
14+
mcbShelf[sys.argv[2]] = pyperclip.paste()
15+
elif sys.argv[1].lower() == 'delete':
16+
del mcbShelf[sys.argv[2]]
17+
elif len(sys.argv) == 2:
18+
# List keywords and load content.
19+
if sys.argv[1].lower() == 'list':
20+
pyperclip.copy(str(list(mcbShelf.keys())))
21+
elif sys.argv[1] in mcbShelf:
22+
pyperclip.copy(mcbShelf[sys.argv[1]])
23+
24+
mcbShelf.close()

08-file/myCats.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cats = [{'desc': 'chubby', 'name': 'Zophie'}, {'desc': 'fluffy', 'name': 'Pooka'}]

08-file/mydata.bak

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
'cats', (0, 45)

0 commit comments

Comments
 (0)