Advertisement
teslariu

archivos

Oct 14th, 2023
952
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.25 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. """
  5. Script que genera un archivo con temperaturas en ºC. Luego, genera otro
  6. similar a partir del primero pero con las temperaturas en ºF
  7.  
  8. funciones: open,close,read,write,readline,writeline,readlines,writelines
  9.  
  10. ========= ===============================================================
  11. Character Meaning
  12. --------- ---------------------------------------------------------------
  13. 'r'       open for reading (default)
  14. 'w'       open for writing, truncating the file first
  15. 'x'       create a new file and open it for writing
  16. 'a'       open for writing, appending to the end of the file if it exists
  17. 'b'       binary mode
  18. 't'       text mode (default)
  19. '+'       open a disk file for updating (reading and writing)
  20. 'U'       universal newline mode (deprecated)
  21. ========= ===============================================================
  22.  
  23. The default mode is 'rt' (open for reading text). For binary random
  24. access, the mode 'w+b' opens and truncates the file to 0 bytes, while
  25. 'r+b' opens the file without truncation. The 'x' mode implies 'w' and
  26. raises an `FileExistsError` if the file already exists.
  27. """
  28. # creo un archivo nuevo "celsius.txt"
  29. try:
  30.     f = open("celsius.txt", "x")
  31. except FileExistsError:
  32.     f = open("celsius.txt","a")
  33.  
  34. # creo una lista de datos
  35. datos = ["10ºC\n","12.5ºC\n","-0.58ºC\n", "215ºC\n", "-25.8ºC\n"]
  36.  
  37. # escribo los datos en el archivo
  38. f.writelines(datos)
  39.  
  40. # cierro el archivo
  41. f.close()
  42.  
  43. # abro de nuevo el archivo para agregarle datos. Esta forma de apertura
  44. # LO CIERRA AUTOMATICAMENTE
  45. with open("celsius.txt","a") as f:
  46.     f.write("11.5ºC\n")
  47.    
  48. # imprimo el contenido
  49. with open("celsius.txt") as f:
  50.     print(f.read())
  51.    
  52. # creo el archivo "farenheit.txt"
  53. try:
  54.     f = open("farenheit.txt", "x")
  55. except FileExistsError:
  56.     f = open("farenheit.txt","a")
  57.    
  58. # abro celsius.txt para leer sus datos
  59. with open("celsius.txt") as f1:
  60.     datos = f1.readlines()
  61.    
  62. # lleno el archivo farenheit con los datos
  63. for dato in datos:
  64.     temp, _ = dato.split("º")
  65.     temp = float(temp) * 1.8 + 32
  66.     f.write(f"{temp:.1f}ºF\n")
  67.    
  68. # cierro el archivo farenheit
  69. f.close()
  70.  
  71. # imprimo el contenido de farenheit
  72. with open("farenheit.txt") as f:
  73.     print(f.read())
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement