Advertisement
teslariu

archivos con y sin with

Aug 15th, 2023 (edited)
1,378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.29 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 (MODO APPENDING)
  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. """
  23. Script que crea un archivo con temp en ºC y luego crea otro con temp
  24. en ºF a partir del primero
  25. """
  26. # para trabajar con archivos: open(), read() write() close()
  27. # menos usadas: writelines(), readlines(), etc
  28.  
  29. # abro un archivo de texto y le agrego datos
  30.  
  31. try:
  32.     f = open("celsius.txt", "x")
  33. except FileExistsError:
  34.     f = open('celsius.txt','a')
  35.    
  36. datos = ["10ºC\n", "-25.3ºC\n", "66ºC\n", "12ºC\n"]
  37.  
  38. # for dato in datos:
  39. #   f.write(dato)
  40.  
  41. f.writelines(datos)
  42.  
  43. # agrego un dato más:()
  44. f.write("100ºC\n")
  45.  
  46. # creo el archivo farenheit
  47. f2 = open("farenheit.txt", "x")
  48.  
  49. # leo los valores de celsius.txt. Como está en modo escritura, lo cierro
  50. # y lo abro en modo lectura
  51. f.close()
  52. f = open("celsius.txt")
  53.  
  54. # imprimo los valores
  55. print(f.read())
  56.  
  57. # cuando leo, se vacía la variable f. Para leer nuevamente, debo cerrar
  58. # y abrir
  59. f.close()
  60. f = open("celsius.txt")
  61.  
  62. # leo los valores y los guardo en una lista
  63. valores = f.readlines()
  64.  
  65. # convierto cada linea de ºC a ºF y la grabo en farenheit.txt
  66. for valor in valores:
  67.     temp, _ = valor.split("º")
  68.     temp = float(temp) * 1.8 + 32
  69.     f2.write(f"{temp:.1f}ºF\n")
  70.  
  71. # cierro los archivos
  72. f.close()
  73. f2.close()
  74.  
  75. """ USO DE WITH
  76. with open("saludo.txt","x") as f:
  77.     f.write("Hola")
  78.    
  79. with open("saludo.txt") as f:
  80.     print(f.read())
  81. """
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement