Advertisement
teslariu

archivos

Jan 22nd, 2022
1,142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.49 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. """
  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.    The default mode is 'rt' (open for reading text). For binary random
  18.    access, the mode 'w+b' opens and truncates the file to 0 bytes, while
  19.    'r+b' opens the file without truncation. The 'x' mode implies 'w' and
  20.    raises an `FileExistsError` if the file already exists.
  21.  
  22. Funciones de manejo de archivo:
  23. open() --> abrir archivos
  24. read() --> leer el contenido
  25. readline() --> leer una línea
  26. readlines() --> leer todas las líneas y guardarlas en un lista
  27. write() --> escribir en un archivo (también hay writeline, etc)
  28. close() --> cierra el archivo
  29.  
  30. """
  31. # Abro un archivo prueba.txt ya existente para lectura:
  32. f = open("prueba.txt")
  33.  
  34. # leo su contenido y lo imprimo
  35. print(f.read())
  36. print()
  37.  
  38. # imprimo una sola linea
  39. f = open("prueba.txt")
  40. print(f.readline())
  41.  
  42. # usando readlines()
  43. f = open("prueba.txt")
  44. print(f.readlines())
  45. print()
  46.  
  47. # como leer 2 lineas:
  48. f = open("prueba.txt")
  49. for i in range(1,3):
  50.     print(f.readline(), end="")
  51.    
  52. # si quiero agregar algo:
  53. f = open("prueba.txt", "a")
  54. f.write("Mercedes\n")
  55.  
  56. # muestro el contenido
  57. f = open("prueba.txt") 
  58. print(f.read())
  59. print()
  60.    
  61.  
  62. # si quiero agregar algo BORRANDO LO QUE HUBIERA:
  63. f = open("prueba.txt", "w")
  64. f.writelines(["Uno\n","Dos\n","Tres\n"])
  65. print()
  66.  
  67.  
  68. # muestro el contenido
  69. f = open("prueba.txt") 
  70. print(f.read())
  71. print()
  72.  
  73. """
  74. # crear un archivo nuevo y agregar datos
  75. alumnos = {"Alejandro":2, "Luis":3, "Ana":4}
  76. f = open("salida_script.txt", "x")
  77.  
  78. for nombre, curso in alumnos.items():
  79.     f.write(f"El alumno {nombre} está inscripto en {curso} cursos\n")
  80.    
  81. # cierro el archivo:
  82. f.close()
  83. """
  84.  
  85. # otra forma preferida de abrir archivos para escribirlos
  86. nombre = "Miguel"
  87. cursos = 10
  88.  
  89. with open("salida_script.txt", "a") as f:
  90.     f.write(f"El alumno {nombre} está inscripto en {cursos} cursos\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement