-
Notifications
You must be signed in to change notification settings - Fork 0
/
filemanager.py
58 lines (49 loc) · 1.72 KB
/
filemanager.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
import os
import time
class FileMananger:
def __init__(self,f_name:str,f_mode="a+"):
self.f_name = f_name
self.f_mode = f_mode
self.file = None
def __enter__(self):
try:
self.file = open(self.f_name,self.f_mode)
return self
except:
self.createFile()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.file.close()
def createFile(self):
if os.path.isfile(self.f_name):
pass
else:
create_file = self.file = open(self.f_name,"w+")
if create_file:
print(f"Created `{self.f_name}` file. \n File mode ` {self.f_mode} .")
def deleteFile(self):
file_to_del = os.remove(self.f_name)
if file_to_del == None:
print(f"Removed `{self.f_name}` file.")
def writeToFile(self,txt):
if self.file.closed == False:
self.file.write(txt)
print(f"`{txt}` added to `{self.f_name}` .")
def readFile(self):
if self.file.closed == False and os.path.isfile(self.f_name):
lines = self.file.readlines()
items: list = list()
for line in lines:
item = line.split(';')
items.append({"name":item[0],"price":item[1],"quantity":item[2]})
return items
if __name__ == "__main__":
file_mananger = FileMananger("log.txt", "r+")
file_mananger.createFile()
with file_mananger as fs:
fs.writeToFile("Indomie;40;2\n")
with file_mananger as flm:
my_shopping_list = flm.readFile()
print(my_shopping_list)
time.sleep(7)
file_mananger.deleteFile()