-
Notifications
You must be signed in to change notification settings - Fork 0
/
21-python-files-exercises.py
61 lines (52 loc) · 1.78 KB
/
21-python-files-exercises.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
from io import open
import sys
def read_people_cvs_exercise():
list_of_keys = ['id', 'name', 'last_name', 'date_of_birth']
try:
file = open("people.csv", "r", encoding="utf8")
lines = file.readlines()
person_list = []
for line in lines:
person_values = line.replace("\n", "").split(",")
person_dictionary = {}
for index, value in enumerate(person_values):
person_dictionary[list_of_keys[index]] = value
person_list.append(person_dictionary)
for person in person_list:
print("[id]={} {} {} => {}".format(person['id'], person['name'], person['last_name'],
person['date_of_birth']))
except Exception as inst:
print(type(inst)) # the exception instance
print(inst.args) # arguments stored in .args
print(inst)
finally:
file.close()
del file
def counter_exercise():
try:
file = open("counter.txt", "a+", encoding="utf8")
file.seek(0)
content = file.readline()
if len(content) <= 0:
content = "0"
file.write(content)
file.close()
counter = int(content)
if len(sys.argv) == 2:
if sys.argv[1] == "inc":
counter += 1
elif sys.argv[1] == "dec" and counter > 0:
counter -= 1
print(counter)
file = open("counter.txt", "w", encoding="utf8")
file.write(str(counter))
file.close()
except Exception as inst:
print(type(inst)) # the exception instance
print(inst.args) # arguments stored in .args
print(inst)
finally:
file.close()
del file
read_people_cvs_exercise()
counter_exercise()