-
Notifications
You must be signed in to change notification settings - Fork 4
/
file_handler.py
33 lines (28 loc) · 1016 Bytes
/
file_handler.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
import os
class fileHandler():
def __init__(self):
pass
def write(text,path):
with open(path,"w",encoding='utf-8') as txt:
txt.write(text)
def read(path):
with open(path,"r",encoding='utf-8') as txt:
return txt.read()
def get_name_dir(name="test", ext="txt", path="."):
"""
-> This function returns "name of file with largest id" which will be saved in selected directory.
"""
files = [f for f in os.listdir(path) if os.path.isfile(os.path.join(path, f))]
id = 0
len_name = len(name)
for f in files:
if name == f[:len_name]:
try:
ind = f.find(".")
if f[ind+1:] == ext:
num = int(f[len_name:ind]) + 1
if num > id:
id = num
except:
pass
return name + str(id) + "." + ext