Advertisement
teslariu

archivos

Jun 27th, 2022
968
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.59 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. """
  5. Funciones básicas para trabajar con archivos: open, write, read, close
  6. Existen funciones especificas: writeline, writelines, readline, readlines
  7.  
  8.  
  9. ========================================================================
  10. Character Meaning
  11. --------- ---------------------------------------------------------------
  12.   'r'       open for reading (default)
  13.   'w'       open for writing, truncating the file first
  14.   'x'       create a new file and open it for writing
  15.   'a'       open for writing, appending to the end of the file if it exists
  16.   'b'       binary mode
  17.   't'       text mode (default)
  18.   '+'       open a disk file for updating (reading and writing)
  19.   'U'       universal newline mode (deprecated)
  20. ========= ===============================================================
  21.  
  22. The default mode is 'rt' (open for reading text). For binary random
  23. access, the mode 'w+b' opens and truncates the file to 0 bytes, while
  24. 'r+b' opens the file without truncation. The 'x' mode implies 'w' and
  25. raises an `FileExistsError` if the file already exists.
  26.  
  27. Python distinguishes between files opened in binary and text modes,
  28. even when the underlying operating system doesn't. Files opened in
  29. binary mode (appending 'b' to the mode argument) return contents as
  30. bytes objects without any decoding. In text mode (the default, or when
  31. 't' is appended to the mode argument), the contents of the file are
  32. returned as strings, the bytes having been first decoded using a
  33. platform-dependent encoding or using the specified encoding if given.
  34.  
  35. """
  36. # Script que crea un archivo vacìo y lo completa con temperaturas en ºC.
  37. # Luego, lo cierra, lo muestra y le agrega datos. Despuès de esto, crea
  38. # otro archivo con las temperaturas del archivo anterior pero en ºF
  39.  
  40. # creo un archivo vacío
  41. f = open("centigrados.txt","x")
  42. datos = ["-10.2ºC\n", "11.2ºC\n","-25.8ºC\n","0ºC\n"]
  43.  
  44. # Guardo los datos en el archivo
  45. # for dato in datos:
  46. #   f.write(dato)
  47. f.writelines(datos)
  48.  
  49. # cierro el archivo
  50. f.close()
  51.  
  52. # lo abro en modo lectura para leer los datos
  53. f = open("centigrados.txt")
  54.  
  55. # imprimo el contenido
  56. # print(f.readlines())
  57.  
  58. for linea in f:
  59.     print(linea.strip())
  60. f.close()
  61.  
  62. # abro para agregar datos
  63. f = open("centigrados.txt","a")
  64. f.write("-10ºC\n")
  65. f.close()
  66.  
  67. # creo el archivo en farenheit
  68. f = open("centigrados.txt")
  69. datos = f.readlines()
  70. f.close()
  71.  
  72. f = open("farenheit.txt","x")
  73. for dato in datos:
  74.     temp,escala = dato.split("º")
  75.     temp = float(temp) * 1.8 + 32
  76.     f.write(f"{temp:.1f}ºF\n")
  77.    
  78. f.close()
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement