Advertisement
teslariu

archivos

Mar 13th, 2021
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.03 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. """
  5. 'r'       open for reading (default)
  6. 'w'       open for writing, truncating the file first
  7. 'x'       create a new file and open it for writing
  8. 'a'       open for writing, appending to the end of the file if it exists
  9. 'b'       binary mode
  10. 't'       text mode (default)
  11. """
  12. # creo un archivo para guardar datos
  13. f = open("datos.txt", "x")
  14.  
  15. # creo una lista con las filas para guardar
  16. lineas = [
  17.         "Listado de nombres\n",
  18.         "------------------\n",
  19.         "Alejandro\n",
  20.         "Ana\n",
  21.         "Luisa\n",
  22.         "Mario\n"
  23.         ]
  24.  
  25.  
  26. # guardo las filas en datos.txt
  27. for linea in lineas:
  28.     f.write(linea)
  29.    
  30. # cierro el archivo
  31. f.close()
  32.  
  33. # abro el archivo para agregar un dato
  34. f = open("datos.txt", "a")
  35.  
  36. # agrego el dato
  37. f.write("Pepito\n")
  38.  
  39. # cierro el archivo
  40. f.close()
  41.  
  42. # abro el archivo para ver su contenido
  43. f = open("datos.txt")
  44.  
  45. # leo el contenido y lo imprimo
  46. lineas = f.readlines()
  47.  
  48. for linea in lineas:
  49.     print(linea,end="")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement