-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path8-Reading-n-Writing-files.py
More file actions
49 lines (33 loc) · 1.36 KB
/
8-Reading-n-Writing-files.py
File metadata and controls
49 lines (33 loc) · 1.36 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
#!/bin/python3
f= open('7-loops.py')
print(f)
#default mode is r, which is read mode
#other modes are appending, write, create mode.
f= open('dummy.txt', 'rt')
print(f)
print("\n-----------------------------------------------\n")
#print(f.read())
print(f.readlines()) #prints all lines as strings in an array format
print("\n-----------------------------------------------\n")
f.seek(0)
for line in f:
print(line.strip()) #reads all lines in the file and strips extra characters
print("\n-----------------------------------------------\n")
print("Closing the file.........")
f.close() #closing the file after use
print("\n-----------------------------------------------\n")
f= open("dummy2.txt", "w") #opening a file in write mode
f.write("Test Line!\n")
f.close
f= open("dummy2.txt", "a") #opening a file in append mode to edit
f.write("Test Line 2!\n")
f.close
print(f.name) #specifies the name of the file in f
print(f.closed) #specifies if the file has been closed or not? Good practice
print(f.mode) #specifies the mode in which the file was last opened in
print("\n-----------------------------------------------\n")
########## ROCKYOU.TXT ############
with open('/usr/share/wordlists/rockyou.txt', encoding='latin-1') as f:
#it is important to mention encoding as some passwords might be encoded
for line in f:
pass #iterating every password in the rockyou.txt file