109 lines
4.7 KiB
Python
109 lines
4.7 KiB
Python
from os import getcwd as pwd
|
|
|
|
class interm005:
|
|
|
|
def nums_txt(self):
|
|
"""Write a new file called \"Numbers.txt\". Add five numbers to the
|
|
document which are stored on the same line and only separated by a
|
|
comma. Once you have run the program, look in the location where your
|
|
program is stored and you should see that the file has been created."""
|
|
from random import randint
|
|
file_path = f"{pwd()}/interm/files/Numbers.txt"
|
|
with open(file_path, 'w') as file:
|
|
for i in range(5):
|
|
if i < 4:
|
|
file.write(str(randint(0,100))+', ')
|
|
else:
|
|
file.write(str(randint(0,100)))
|
|
print(f"Puedes encontrar el archivo en '{file_path}'")
|
|
|
|
def names_txt(self):
|
|
"""Create a new file called \"Names.txt\". Add five names to the
|
|
document, which are stored on separate lines. Once you have run the
|
|
program, look in the location where your program is stored and check
|
|
that the file has been created properly."""
|
|
file_path = f"{pwd()}/interm/files/Names.txt"
|
|
names = ( 'Juana', 'Roberta', 'Manuelo', 'Genialo', 'Zerafin' )
|
|
with open(file_path, 'w') as file:
|
|
file.writelines('\n'.join(names))
|
|
file.write('\n')
|
|
print(f"Puedes encontrar el archivo en '{file_path}'")
|
|
|
|
def read_names(self):
|
|
"""Open the \"Names.txt\" file and display the data in Python."""
|
|
file_path = f"{pwd()}/interm/files/Names.txt"
|
|
with open(file_path, 'r') as file:
|
|
print(file.read())
|
|
|
|
def add_name(self):
|
|
"""Open the \"Names.txt\" file. Ask the user to input a new name. Add
|
|
this to the end of the file and display the entire file."""
|
|
file_path = f"{pwd()}/interm/files/Names.txt"
|
|
name = input("Ingresa un nombre para agregarlo al archivo 'Names.txt': ")
|
|
with open(file_path, 'a') as file:
|
|
file.write(name+'\n')
|
|
self.read_names()
|
|
|
|
def menu(self):
|
|
"""Display the following menu to the user:
|
|
1) Create a new file
|
|
2) Display the file
|
|
3) Add a new item to the file
|
|
|
|
Make a selection 1, 2 or 3:
|
|
Ask the user to enter 1, 2 or 3. If they select anything other than 1,
|
|
2 or 3 it should display a suitable error message. If they select 1,
|
|
ask the user to enter a school subject and save it to a new file called
|
|
\"Subject.txt\". It should overwrite any existing file with a new file.
|
|
If they select 2, display the contents of the \"Subject.txt\" file. If
|
|
they select 3, ask the user to enter a new subject and save it to the
|
|
file and then display the entire contents of the file. Run the program
|
|
several times to test the options."""
|
|
file_path = f"{pwd()}/interm/files/Subject.txt"
|
|
msg = "Ingresa una asignatura para agregar al archivo 'Subject.txt': "
|
|
menu = """
|
|
1) Crear un nuevo archivo
|
|
2) Mostrar el contenido del archivo
|
|
3) Agregar un nuevo elemento al archivo
|
|
"""
|
|
print(menu)
|
|
sel = int(input(" Ingresa una opción (1-3): "))
|
|
match sel:
|
|
case 1:
|
|
item = input(msg)
|
|
with open(file_path, 'w') as file:
|
|
file.write(item+'\n')
|
|
print(f"Puedes encontrar el archivo en '{file_path}'")
|
|
case 2:
|
|
with open(file_path, 'r') as file:
|
|
print(file.read())
|
|
case 3:
|
|
item = input(msg)
|
|
with open(file_path, 'a') as file:
|
|
file.write(item+'\n')
|
|
with open(file_path, 'r') as file:
|
|
print(file.read())
|
|
case _:
|
|
print("Debes ingresar una opción válida")
|
|
|
|
def names2(self):
|
|
"""Using the \"Names.txt\" file you created earlier, display the
|
|
list of names in Python. Ask the user to type in one of the names
|
|
and then save all the names except the one they entered into a new
|
|
file called \"Names2.txt\"."""
|
|
file_path1 = f"{pwd()}/interm/files/Names.txt"
|
|
file_path2 = f"{pwd()}/interm/files/Names2.txt"
|
|
names = []
|
|
with open(file_path1, 'r') as file:
|
|
names = file.readlines()
|
|
for name in names:
|
|
print(name.replace('\n',''))
|
|
sel = input("Ingresa el nombre a excluir en \"Names2.txt\": ")+'\n'
|
|
with open(file_path2, 'w') as file:
|
|
for name in names:
|
|
if sel != name:
|
|
file.write(name)
|
|
print(f"\nPuedes encontrar el archivo en '{file_path2}'\n")
|
|
with open(file_path2, 'r') as file:
|
|
print(file.read())
|