-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_manager.py
61 lines (53 loc) · 2.06 KB
/
file_manager.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
59
60
61
import os
class FileMngr():
def checkFilePath(self, file_path):
return os.path.exists(file_path)
def openFileForReading(self, file_path):
if not self.checkFilePath(file_path):
return f'File {file_path} does not exist'
try:
file_reader = open(file_path,'r')
return file_reader
except Exception as ex:
return f'An error occurred: {ex}'
def readFromFilePathOpenClose(self, file_path):
if not self.checkFilePath(file_path):
return f'File {file_path} does not exist'
try:
with open(file_path,'r') as fileReader:
return fileReader.read()
except Exception as ex:
return f'An error occurred while trying to read file: {ex}'
def readLineWithFileReader(self, lineReader):
try:
line = lineReader.readline()
if line.endswith('\n'):
line = line.rstrip('\n')
return line
except Exception as ex:
return f'An error occurred while trying to read a line: {ex}'
def openFileForWriting(self, file_path):
try:
file_writer = open(file_path,'w')
return file_writer
except Exception as ex:
return f'An error occurred: {ex}'
def writeToFilePath(self, file_path, content):
try:
with open(file_path,'a') as fileWriter:
fileWriter.write(content)
except Exception as ex:
return f'An error occurred while trying to write: {ex}'
def writeLineWithFileWriter(self, lineWriter, lineContent):
try:
lineContent = lineContent + '\n'
lineWriter.write(lineContent)
except Exception as ex:
return f'An error occurred while trying to write a line: {ex}'
def deleteCSVFile(self, file_path):
if not self.checkFilePath(file_path):
return f'File {file_path} does not exist'
try:
os.remove(file_path)
except Exception as ex:
return f'An error occurred: {ex}'