-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_file.py
28 lines (21 loc) · 913 Bytes
/
read_file.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
import re
def read_file(num):
with open("D:\Download\DiscordBot_EHC_Workshop\\text.txt", "r") as f:
lines = f.readlines()
global CURRENT_NUMBER_OF_LINES
CURRENT_NUMBER_OF_LINES = len(lines) #get current number of lines in the text file
for str in lines: # for each lines in the tuple find the matching pattern
match_object = re.search(pattern=f"^Line {num}.+", string=str)
if match_object:
return match_object.group()[8:]
else:
continue
def write_file():
read_file(-1) # call to get number of lines in the file
with open("D:\Download\DiscordBot_EHC_Workshop\\text.txt", "a") as f:
user_input = input("Enter string: ")
temp = CURRENT_NUMBER_OF_LINES +1
f.write(f"\nLine {temp}. "+user_input)
if __name__ == '__main__':
print(read_file(1))
# write_file()