Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.
68 changes: 68 additions & 0 deletions src/exerciseeight/filemanage.py
Original file line number Diff line number Diff line change
@@ -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 _closeFile(self):
"""Private method to close a file
:return: None
:rtype: NoneType
"""
self._file.close()
self._file = None

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 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
:type text: str
:return: None
:rtype: NoneType
"""
self._openTextFile('wt')
self._file.write(text)
self._closeFile()
2 changes: 2 additions & 0 deletions src/files/path.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
PATH=src/files/
FILENAME=file.txt
68 changes: 64 additions & 4 deletions src/main.py
Original file line number Diff line number Diff line change
@@ -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))