Advertisement
teslariu

archivos

Nov 23rd, 2021
988
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.35 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. """
  5. funciones para archivo: open(), read(), write(), close()
  6.  
  7. ========= ===============================================================
  8.    Character Meaning
  9.    --------- ---------------------------------------------------------------
  10.    'r'       open for reading (default)
  11.    'w'       open for writing, truncating the file first
  12.    'x'       create a new file and open it for writing
  13.    'a'       open for writing, appending to the end of the file if it exists
  14.    'b'       binary mode
  15.    't'       text mode (default)
  16.    '+'       open a disk file for updating (reading and writing)
  17.    'U'       universal newline mode (deprecated)
  18. ========= ===============================================================
  19.  
  20. The default mode is 'rt' (open for reading text).
  21. """
  22. # Script que crea un archivo de texto con temperaturas en ºC
  23. # y otro con las mismas temperaturas expresadas en ºF
  24.  
  25. # creo un archivo vacio 'celsius.txt' para escritura
  26. f = open('celsius.txt','x')
  27.  
  28. # creo una lista con los datos para agregar al archivo
  29. datos = ["20ºC\n", "-15ºC\n", "11ºC\n", "0ºC\n"]
  30.  
  31. # escribo los datos en el archivo
  32. f.writelines(datos)
  33.  
  34. """
  35. otra forma:
  36. for dato in datos:
  37.     f.write(dato)
  38. """
  39. # cierro el archivo porque termine de guardar los datos
  40. f.close()
  41.  
  42. # abro de nuevo para agregar otro dato
  43. f = open("celsius.txt","a")
  44. f.write("100ºC\n")
  45. f.close()
  46.  
  47. #agrego una dato arriba de todo
  48. #primero abro(modo lectura)para leer los valores
  49. f = open("celsius.txt")
  50. datos = f.readlines()
  51. f.close()
  52.  
  53. # ahora abro modo escritura para escribir los valores
  54. f = open("celsius.txt","w")
  55. datos.insert(0,"33ºC\n")
  56. f.writelines(datos)
  57. f.close()
  58.  
  59. # creo un archivo vacio 'farenheit.txt' para escritura
  60. f = open('farenheit.txt','x')
  61.  
  62. # convierto los datos a farenheit y los grabo
  63. for dato in datos:
  64.     temp, escala = dato.split("º")
  65.     temp = float(temp) * 1.8 + 32
  66.     f.write("{:.1f}ºF\n".format(temp))
  67.    
  68. # cierro el archivo
  69. f.close()
  70.  
  71.  
  72. """
  73. f = open("demo.txt")
  74. print(f.read()) --> imprime todo el contenido de demo.txt
  75. print(f.readline())  --> imprime una linea
  76.  
  77. Manera recomendada para abrir archivos, modificarlo/añadir contenido
  78. with open("demo.txt","a") as f:  # equivale a f = open("demo.txt","a")
  79.     f.write("nueva linea\n")
  80.    
  81. (se cierra automaticamente, no necesita close()
  82.  
  83. """
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement