-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmy_first_file.py
More file actions
59 lines (43 loc) · 1.17 KB
/
my_first_file.py
File metadata and controls
59 lines (43 loc) · 1.17 KB
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
""" Files examples
"""
import os
def first():
with open('experience.txt', 'w') as f:
f.write('This is my first saved output\n')
def second():
# Lembrando que se o W for informado incorretamente e o arquivo já exister,
# O ARQUIVO SERÁ SOBREESCRITO. OVERWRITTEN!!!
with open('experience.txt', 'a') as f:
f.write('This is my second saved output\n')
# Let's try with some numbers:
def numbers():
l = list()
for i in range(10):
l.append(i ** i)
print(sum(l))
with open('experience.txt', 'a') as f:
for each in l:
f.write(f'{each};')
# What have we done?
def reading():
with open('experience.txt', 'r') as f:
lida = f.read()
print(lida)
return lida
def recover_list(li):
li = li.split('\n')
recovered = list()
for each in li[2].split(';'):
if len(each) > 0:
recovered.append(int(each))
print(recovered)
return recovered
def sum_list(recovered):
print('A soma da lista recuperada é {:,.0f}'.format(sum(recovered)))
if __name__ == '__main__':
first()
second()
numbers()
r = reading()
ll = recover_list(r)
sum_list(ll)