-
Notifications
You must be signed in to change notification settings - Fork 0
/
handleFiles&Json.py
73 lines (59 loc) · 1.64 KB
/
handleFiles&Json.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
62
63
64
65
66
67
68
69
70
71
72
73
import os
import shutil
oriPath = os.path.dirname(os.path.abspath(__file__))
newPath = input('\tNew Path: ')
ext = input('\n\text of files: ')
try:
os.mkdir(newPath)
except FileExistsError as error:
print(f'Path {newPath} exists!')
for root, dirs, files in os.walk(oriPath):
for file in files:
old_FilePath = os.path.join(root, file)
new_FilePath = os.path.join(newPath, file)
if ext in file:
# move, copy or os.remove
shutil.copy(old_FilePath, new_FilePath)
print(f'File {file} was copied with success!')
# 'r' -> Usado somente para ler algo
# 'w' -> usado somente para escrever algo
# 'w+' -> usado para ler e escrever arquivo
# 'r+' -> Usado para ler e escrever algo
# 'a+' -> Usado para acrescentar algo sem apagar
# close file automatically
with open('test.txt', 'w+') as file:
file.write('Line 1\n')
file.write('Line 2\n')
file.seek(0,0)
for line in file:
print(line, end='')
os.remove('test.txt')
# finally:
# file.close()
import json
print('SAVE JSON FILE')
# json file
d1 = {
'Pessoa 1': {
'nome': 'Luiz',
'idade': 25,
},
'Pessoa 2': {
'nome': 'Rose',
'idade': 30,
},
}
d1_json = json.dumps(d1, indent=True)
with open ('test.json', 'w+') as file:
file.write(d1_json)
print(d1_json)
#### read json ####
print('READ JSON FILE')
with open ('test.json', 'r') as file:
d1_json = file.read()
d1_json = json.loads(d1_json)
for k, v in d1_json.items():
print(k)
for k1, v1 in v.items():
print(k1, v1)
print()