Advertisement
teslariu

errores files

Oct 25th, 2021
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.56 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Script para agregar una línea de texto que diga hola a un archivo
  4. # uno.txt, si el archivo no existe lo crea
  5.  
  6.  
  7.  
  8. try:      
  9.     f = open("uno.txt", "x")            # intento hacer esto
  10. except FileExistsError:
  11.     f = open("uno.txt","a")             # ejecuto esto si da error
  12. else:
  13.     print("Creando archivo uno.txt")    # ejecuto esto si no da error
  14. finally:
  15.     f.write("Hola\n")                   # ejecuto esto
  16.     f.close()                           # siempre
  17.    
  18.    
  19. """
  20. ejecuta try --> else  --> finally si no hay error
  21. ejecuta try --> except --> finally si da error
  22. """
  23.    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement