Advertisement
teslariu

arc

Sep 4th, 2021
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.76 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. """
  5. ====== ===============================================================
  6. Character Meaning
  7. --------- ---------------------------------------------------------------
  8. 'r'       open for reading (default)
  9. 'w'       open for writing, truncating the file first
  10. 'x'       create a new file and open it for writing
  11. 'a'       open for writing, appending to the end of the file if it exists
  12. 'b'       binary mode
  13. 't'       text mode (default)
  14. '+'       open a disk file for updating (reading and writing)
  15. 'U'       universal newline mode (deprecated)
  16. ========= ===============================================================
  17.  
  18. The default mode is 'rt'
  19.  
  20. """
  21. # funciones de archivos: open(), read(), write(), close()
  22. # Programa que crea un archivo con temp en ºC, lo guarda y lo cierra
  23. # Luego lo abre, le agrega nuevos datos, lo guarda y lo cierra
  24. # Luego, abre el archivo, lee los datos y crea otro archivo similar pero en ºF
  25.  
  26. # Creo un archivo en modo texto para escribirlo
  27. f = open("centigrados.txt","x")
  28.  
  29. # creo una lista con los datos a guardar en el archivo
  30. datos = ["25ºC\n", "14ºC\n", "0.25ºC\n", "-45ºC\n"]
  31.  
  32. # escribo los datos en el archivo (y los guardo)
  33. for dato in datos:
  34.     f.write(dato)
  35.    
  36. # cierro el archivo
  37. f.close()
  38.  
  39. # abro el archivo para agregar datos (append) y lo cierro
  40. f = open("centigrados.txt", "a")
  41. datos = ["17.25ºC\n", "-18ºC\n"]
  42. f.writelines(datos)
  43. f.close()
  44.  
  45. # abro el archivo en modo lectura
  46. f = open("centigrados.txt")
  47. datos = f.readlines()
  48. f.close()
  49.  
  50. # creo el archivo farenheit.txt
  51. f = open("farenheit.txt","x")
  52. for linea in datos:
  53.     temp, escala = linea.split("º")
  54.     temp = float(temp) * 1.8 + 32
  55.     f.write("{:.1f}ºF\n".format(temp))
  56.  
  57. # cierro el archivo
  58. f.close()
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement