From c3f5aac56f3dfed8950f84201cb0dd2f8d1550e7 Mon Sep 17 00:00:00 2001 From: Izri Toufali Lapaz Date: Thu, 10 Nov 2022 12:09:42 +0100 Subject: [PATCH 1/3] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index fa0237d..3a434d2 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,4 @@ # OpenBootCampPython Repository to store Python boot camp. + +Ejercicio 8.A: En este ejercicio, tendréis que crear un archivo py donde creéis un archivo txt, lo abráis y escribáis dentro del archivo. Para ello, tendréis que acceder dos veces al archivo creado. From c69f5f7bd9379cd900bae3fc5d46a958ff0cd1b9 Mon Sep 17 00:00:00 2001 From: tlizri Date: Thu, 10 Nov 2022 14:40:36 +0100 Subject: [PATCH 2/3] Completed exercise eight A --- src/exerciseeight/filemanage.py | 68 +++++++++++++++++++++++++++++++++ src/files/path.txt | 2 + src/main.py | 68 +++++++++++++++++++++++++++++++-- 3 files changed, 134 insertions(+), 4 deletions(-) create mode 100644 src/exerciseeight/filemanage.py create mode 100644 src/files/path.txt diff --git a/src/exerciseeight/filemanage.py b/src/exerciseeight/filemanage.py new file mode 100644 index 0000000..3c1fd75 --- /dev/null +++ b/src/exerciseeight/filemanage.py @@ -0,0 +1,68 @@ +class FileManager: + """Conceptual representation of a file manager + :param _path: path.txt pointed to file directory + :type _path: str + :param _filename: file name + :type _filename: str + :param _file: text input output object + :type _file: TextIO + """ + _path = None + _filename = None + _file = None + + def __init__(self, path: str, filename: str): + """Constructor method + :param path: path.txt pointed to file directory + :type path: str + :param filename: file name + :type filename: str + """ + self._path = path + self._filename = filename + + def __del__(self): + """Destructor method + :return: None + :rtype: NoneType + """ + del self._path, self._filename, self._file + + def createTextFile(self): + """Method to create a text file + :return: None + :rtype: NoneType + """ + try: + self._openTextFile(mode='xt') + self._closeFile() + except FileExistsError: + pass + + def _openTextFile(self, mode: str): + """Private method to open a text file + :param mode: mode to open the text file + :type mode: str + :return: None + :rtype: NoneType + """ + self._file = open(f'{self._path}{self._filename}', mode) + + def writeTextFile(self, text: str): + """Method that open a text file, delete its content, write text and close + :param text: text to be written in the text file + :type text: str + :return: None + :rtype: NoneType + """ + self._openTextFile('wt') + self._file.write(text) + self._closeFile() + + def _closeFile(self): + """Private method to close a file + :return: None + :rtype: NoneType + """ + self._file.close() + self._file = None diff --git a/src/files/path.txt b/src/files/path.txt new file mode 100644 index 0000000..97dc222 --- /dev/null +++ b/src/files/path.txt @@ -0,0 +1,2 @@ +PATH=src/files/ +FILENAME=file.txt \ No newline at end of file diff --git a/src/main.py b/src/main.py index a47b6a3..c2bdcf7 100644 --- a/src/main.py +++ b/src/main.py @@ -1,10 +1,70 @@ -def main(): - """Main script for pythonBootCamp project +# LIBRARIES +from sys import argv + +from exerciseeight.filemanage import FileManager + +# CONSTANT +PATH = 'src/files/path.txt' + + +# FUNCTIONS +def argsTransformation(args: list[str], argc: int) -> str: + """Function to process main arguments in a sigle string + :param args: string list to be processed + :type args: list[str] + :param argc: size of args + :type argc: int + :return: string list in a unique string + :rtype: str + """ + text = '' + for arg in args[1:argc]: + if arg == args[argc - 1]: + text += arg + else: + text += arg + ' ' + text += '\n' + return text + + +def readPaths() -> tuple[str, str]: + """Function to read a file with path.txt data + :return: return path.txt and filename + :rtype: tuple[str, str] + """ + f = open(PATH, 'r') + paths = f.readlines() + f.close() + split = [] + for path in paths: + split.append(path.split('=')) + path = '' + filename = '' + for linea in split: + if linea[0] == 'PATH': + path = linea[1].replace('\n', '') + if linea[0] == 'FILENAME': + filename = linea[1].replace('\n', '') + return path, filename + + +# MAIN FUNCTION +def main(args, argc): + """Main script for exercise eight A :return: None :rtype: NoneType """ - pass + # Convert arguments in one string + text = argsTransformation(args, argc) + # Take path.txt and filename from files/path.txt + path, filename = readPaths() + # Create file manager object + f = FileManager(path, filename) + # Create text file + f.createTextFile() + # Write arguments in text file + f.writeTextFile(text) if __name__ == '__main__': - main() + main(argv, len(argv)) From 1af28d314954f77e8c20037e453de20f17497f50 Mon Sep 17 00:00:00 2001 From: tlizri Date: Thu, 10 Nov 2022 14:53:33 +0100 Subject: [PATCH 3/3] Reorganized FileManager methods --- src/exerciseeight/filemanage.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/src/exerciseeight/filemanage.py b/src/exerciseeight/filemanage.py index 3c1fd75..3f14fc8 100644 --- a/src/exerciseeight/filemanage.py +++ b/src/exerciseeight/filemanage.py @@ -28,16 +28,13 @@ def __del__(self): """ del self._path, self._filename, self._file - def createTextFile(self): - """Method to create a text file + def _closeFile(self): + """Private method to close a file :return: None :rtype: NoneType """ - try: - self._openTextFile(mode='xt') - self._closeFile() - except FileExistsError: - pass + self._file.close() + self._file = None def _openTextFile(self, mode: str): """Private method to open a text file @@ -48,6 +45,17 @@ def _openTextFile(self, mode: str): """ self._file = open(f'{self._path}{self._filename}', mode) + def createTextFile(self): + """Method to create a text file + :return: None + :rtype: NoneType + """ + try: + self._openTextFile(mode='xt') + self._closeFile() + except FileExistsError: + pass + def writeTextFile(self, text: str): """Method that open a text file, delete its content, write text and close :param text: text to be written in the text file @@ -58,11 +66,3 @@ def writeTextFile(self, text: str): self._openTextFile('wt') self._file.write(text) self._closeFile() - - def _closeFile(self): - """Private method to close a file - :return: None - :rtype: NoneType - """ - self._file.close() - self._file = None