Advertisement
teslariu

archivos

May 15th, 2021
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.38 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Help de open()
  4. #=======================================================================
  5. #    Character Meaning
  6. #    --------- ---------------------------------------------------------
  7. #    'r'       open for reading (default)
  8. #    'w'       open for writing, truncating the file first
  9. #    'x'       create a new file and open it for writing
  10. #    'a'       open for writing, appending to the end of the file if it exists
  11. #    'b'       binary mode
  12. #    't'       text mode (default)
  13. #    '+'       open a disk file for updating (reading and writing)
  14. #    'U'       universal newline mode (deprecated)
  15. #=======================================================================
  16.  
  17. # Creo un archivo llamado "archivo.txt"
  18. f = open("archivo.txt","x")
  19.  
  20. # Le escribo un título:
  21. f.write("Lista de compras\n")
  22. f.write("----------------\n")
  23.  
  24. # creo una lista de cosas a comprar:
  25. lista = ["1 kg de papas\n", "2 kg de asado\n", "1/2 kg de zanahorias\n"]
  26.  
  27. # agrego la lista al archivo:
  28. for elemento in lista:
  29.     f.write(elemento)
  30.    
  31. # cierro el archivo
  32. f.close()
  33.  
  34. # abro el archivo en modo lectura para imprimirlo
  35. f = open("archivo.txt")
  36. print(f.read())
  37.  
  38. # cierro el archivo
  39. f.close()
  40.  
  41. # lo abro en modo append para agregarle una compra
  42. f = open("archivo.txt","a")
  43. f.write("COMPRAR VINO Y CERVEZA...\n")
  44.  
  45. # cierro el archivo
  46. f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement