Skip to content

Commit 74df00e

Browse files
committed
Completed exercise eight A
1 parent 5a3f847 commit 74df00e

3 files changed

Lines changed: 134 additions & 4 deletions

File tree

src/exerciseeight/filemanage.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
class FileManager:
2+
"""Conceptual representation of a file manager
3+
:param _path: path.txt pointed to file directory
4+
:type _path: str
5+
:param _filename: file name
6+
:type _filename: str
7+
:param _file: text input output object
8+
:type _file: TextIO
9+
"""
10+
_path = None
11+
_filename = None
12+
_file = None
13+
14+
def __init__(self, path: str, filename: str):
15+
"""Constructor method
16+
:param path: path.txt pointed to file directory
17+
:type path: str
18+
:param filename: file name
19+
:type filename: str
20+
"""
21+
self._path = path
22+
self._filename = filename
23+
24+
def __del__(self):
25+
"""Destructor method
26+
:return: None
27+
:rtype: NoneType
28+
"""
29+
del self._path, self._filename, self._file
30+
31+
def createTextFile(self):
32+
"""Method to create a text file
33+
:return: None
34+
:rtype: NoneType
35+
"""
36+
try:
37+
self._openTextFile(mode='xt')
38+
self._closeFile()
39+
except FileExistsError:
40+
pass
41+
42+
def _openTextFile(self, mode: str):
43+
"""Private method to open a text file
44+
:param mode: mode to open the text file
45+
:type mode: str
46+
:return: None
47+
:rtype: NoneType
48+
"""
49+
self._file = open(f'{self._path}{self._filename}', mode)
50+
51+
def writeTextFile(self, text: str):
52+
"""Method that open a text file, delete its content, write text and close
53+
:param text: text to be written in the text file
54+
:type text: str
55+
:return: None
56+
:rtype: NoneType
57+
"""
58+
self._openTextFile('wt')
59+
self._file.write(text)
60+
self._closeFile()
61+
62+
def _closeFile(self):
63+
"""Private method to close a file
64+
:return: None
65+
:rtype: NoneType
66+
"""
67+
self._file.close()
68+
self._file = None

src/files/path.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
PATH=src/files/
2+
FILENAME=file.txt

src/main.py

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,70 @@
1-
def main():
2-
"""Main script for pythonBootCamp project
1+
# LIBRARIES
2+
from sys import argv
3+
4+
from exerciseeight.filemanage import FileManager
5+
6+
# CONSTANT
7+
PATH = 'src/files/path.txt'
8+
9+
10+
# FUNCTIONS
11+
def argsTransformation(args: list[str], argc: int) -> str:
12+
"""Function to process main arguments in a sigle string
13+
:param args: string list to be processed
14+
:type args: list[str]
15+
:param argc: size of args
16+
:type argc: int
17+
:return: string list in a unique string
18+
:rtype: str
19+
"""
20+
text = ''
21+
for arg in args[1:argc]:
22+
if arg == args[argc - 1]:
23+
text += arg
24+
else:
25+
text += arg + ' '
26+
text += '\n'
27+
return text
28+
29+
30+
def readPaths() -> tuple[str, str]:
31+
"""Function to read a file with path.txt data
32+
:return: return path.txt and filename
33+
:rtype: tuple[str, str]
34+
"""
35+
f = open(PATH, 'r')
36+
paths = f.readlines()
37+
f.close()
38+
split = []
39+
for path in paths:
40+
split.append(path.split('='))
41+
path = ''
42+
filename = ''
43+
for linea in split:
44+
if linea[0] == 'PATH':
45+
path = linea[1].replace('\n', '')
46+
if linea[0] == 'FILENAME':
47+
filename = linea[1].replace('\n', '')
48+
return path, filename
49+
50+
51+
# MAIN FUNCTION
52+
def main(args, argc):
53+
"""Main script for exercise eight A
354
:return: None
455
:rtype: NoneType
556
"""
6-
pass
57+
# Convert arguments in one string
58+
text = argsTransformation(args, argc)
59+
# Take path.txt and filename from files/path.txt
60+
path, filename = readPaths()
61+
# Create file manager object
62+
f = FileManager(path, filename)
63+
# Create text file
64+
f.createTextFile()
65+
# Write arguments in text file
66+
f.writeTextFile(text)
767

868

969
if __name__ == '__main__':
10-
main()
70+
main(argv, len(argv))

0 commit comments

Comments
 (0)