From 7c2ccb390c52b94820cf33040160b2a63ae185bd Mon Sep 17 00:00:00 2001 From: Manuel Vergara Date: Mon, 8 Jan 2024 22:36:16 +0100 Subject: [PATCH] Update Python Ofensivo --- .../00_ejercicios/08_entrada_salida01.py | 70 +++++++++++++++++++ .../00_ejercicios/08_entrada_salida02.py | 32 +++++++++ 2 files changed, 102 insertions(+) create mode 100644 python-ofensivo/00_ejercicios/08_entrada_salida01.py create mode 100644 python-ofensivo/00_ejercicios/08_entrada_salida02.py diff --git a/python-ofensivo/00_ejercicios/08_entrada_salida01.py b/python-ofensivo/00_ejercicios/08_entrada_salida01.py new file mode 100644 index 0000000..4703cf7 --- /dev/null +++ b/python-ofensivo/00_ejercicios/08_entrada_salida01.py @@ -0,0 +1,70 @@ +#!/usr/bin/env python3 +""" +Pruebas con open de Python + +w = write +r = read +a = append +b = binary ++ = read and write +""" + +f = open('example.txt', 'w') # Cuidado! Que borra lo que haya en el fichero +f.write('¡Hola mundo!\n') +f.write('k ase') +f.close() + +f = open('example.txt', 'a') +f.write('\n¡Hasta luego mundo!\n') +f.close() + +# Lo más óptimo es usar with +# De esta manera se cierra automáticamente el fichero + +with open('example.txt', 'r') as f: + file_content = f.read() + +print(file_content) + + +# Se puede leer línea a línea +# end='' para que no haga un salto de línea +# line.strip() para quitar los espacios en blanco + +with open('/etc/hosts', 'r') as f: + for line in f: + print(line, end='') + +# Se puede crear una lista con el contenido del fichero + +mi_lista = [ + "Primera línea\n", + "Segunda línea\n", + "Tercera línea\n", + "Cuarta línea\n" +] + +with open('example.txt', 'w') as f: + f.writelines(mi_lista) + +with open('example.txt', 'r') as f: + for line in f: + print(line, end='') + +# with open("/etc/passwd", "r") as f: +# for line in f.readlines(): # Abre todo el fichero y lo mete en una lista +# print(line, end='') + +print() + +# with open("/etc/passwd", "r") as f: +# for line in f.readline(): # Abre solo la primera línea +# print(line, end='') + + +with open("example.txt", "w") as f: + print("Primera línea", file=f) # También se puede guardar así + +with open("example.txt", "r") as f: + for line in f: + print(line, end='') diff --git a/python-ofensivo/00_ejercicios/08_entrada_salida02.py b/python-ofensivo/00_ejercicios/08_entrada_salida02.py new file mode 100644 index 0000000..3889436 --- /dev/null +++ b/python-ofensivo/00_ejercicios/08_entrada_salida02.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python3 +""" +Pruebas con open de Python + +w = write +r = read +a = append +b = binary ++ = read and write +""" + +# Abrir fichero binario y guardarlo en otro +# imagen + +with open("/home/v/Imatges/salvapantallas/peces.jpg", "rb") as f_in, open("image.png", "wb") as f_out: + file_content = f_in.read() + f_out.write(file_content) + + +# Antes +try: + with open("test.txt", "r") as f: + print(f.read()) +except IOError: + print("\n[!] Error: No se ha podido abrir el fichero") + +# Ahora +try: + with open("test.txt", "r") as f: + print(f.read()) +except FileNotFoundError: + print("\n[!] Error: No se ha podido abrir el fichero")