-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiles.py
57 lines (41 loc) · 1.42 KB
/
files.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
import json
inputfile = "/Users/isakonst/Learn1/Python/justfile2.txt"
outputfile = "/Users/isakonst/Learn1/Python/justfile.txt"
password_tolookfor = "password"
myfile1 = open(inputfile, mode='r', encoding='latin_1') # r - read, w- write, a - append, r+
myfile2 = open(outputfile, mode='a', encoding='latin_1')
for num, line in enumerate(myfile1, 1):
if password_tolookfor in line:
print("Line N: " + str(num) + " : " + line.strip())
myfile2.write("Found password:" + line)
myfile1.close()
myfile2.close()
######
filename = "user_settings.txt"
myfile = open(filename, mode='w', encoding='Latin-1')
player1 = {
'PlayerName': "Donald Trump",
'Score': 345,
'awards': ["OR", "NV", "NY"]
}
player2 = {
'PlayerName': "Clinton Hillary",
'Score': 346,
'awards': ["WT", "TX", "MI"]
}
myplayers = []
myplayers.append(player1)
myplayers.append(player2)
# ------------ SAVE by JSON -----------
json.dump(myplayers, myfile)
myfile.close()
# -------- LAOD by JSON --------
myfile = open(filename, mode='r', encoding='Latin-1')
json_data = json.load(myfile)
for user in json_data:
print("Player Name is: " + str(user['PlayerName']))
print("Player Score is: " + str(user['Score']))
print("Player Award N1: " + str(user['awards'][0]))
print("Player Award N2: " + str(user['awards'][1]))
print("Player Award N3: " + str(user['awards'][2]))
print("----------------------------------\n\n")