Advertisement
teslariu

archivos

Aug 17th, 2022
929
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.17 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' (open for reading text). For binary random
  19.    access, the mode 'w+b' opens and truncates the file to 0 bytes, while
  20.    'r+b' opens the file without truncation. The 'x' mode implies 'w' and
  21.    raises an `FileExistsError` if the file already exists.
  22.  
  23.    Python distinguishes between files opened in binary and text modes,
  24.    even when the underlying operating system doesn't. Files opened in
  25.    binary mode (appending 'b' to the mode argument) return contents as
  26.    bytes objects without any decoding. In text mode (the default, or when
  27.    't' is appended to the mode argument), the contents of the file are
  28.    returned as strings, the bytes having been first decoded using a
  29.    platform-dependent encoding or using the specified encoding if given.
  30.  
  31. open() --> abre un archivo (si no existe lo puede crear)
  32. close()  --> cierra un archivo
  33. read()  --> lee el contenido de un archivo
  34. write() --> escribe el contenido en un archivo
  35. readline, readlines, writeline, writelines: manejan archivos como si
  36.      fueran listas de renglones
  37. ej: numeros.txt
  38.    1
  39.    2
  40.    3
  41.  
  42. readline()  --> lee [1]
  43. readlines() --> lee [1,2,3]
  44.  
  45. """
  46.  
  47. # Script que crea un archivo de texto con temperaturas en ºC y luego
  48. # crea otro equivalente en ºF
  49.  
  50. # Creo un archivo vacío celsius.txt
  51. try:
  52.     f = open("celsius.txt", "x")
  53. except FileExistsError:
  54.     f = open("celsius.txt", "w")
  55.  
  56. # creo una lista de datos para guardar en celsius.txt
  57. datos = ["10ºC\n","-14.2ºC\n","0.1ºC\n","-3ºC\n","11ºC\n"]
  58.  
  59. # agrego los datos al archivo:
  60. #for dato in datos:
  61. #    f.write(dato)
  62. #f.close()
  63. f.writelines(datos)
  64. f.close()
  65.  
  66. # lo abro para leer e imprimirlo
  67. f = open("celsius.txt")
  68. print(f.read())
  69.  
  70. # Supongamos que me hubiera olvidado un dato, y se lo quiero añadir:
  71. f.close()
  72. f = open("celsius.txt","a")
  73. f.write("100ºC\n")
  74. f.close()
  75.  
  76. # Debo leer nuevamente el archivo celsius.txt para crear farenheit.txt
  77. f = open("celsius.txt")
  78. datos = f.readlines()
  79. f.close()
  80.  
  81. # creo farenheit.txt
  82. f = open("farenheit.txt","x")
  83.  
  84. # convierto a ºF las temperaturas y las guardo
  85. for dato in datos:
  86.     temp, escala = dato.split("º")
  87.     temp = float(temp) * 1.8 + 32
  88.     f.write(f"{temp:.1f}ºF\n")
  89. f.close()
  90.    
  91. """
  92. 1) Creo un archivo y le escribo "Hola"
  93. f = open("archivo.txt", "x")
  94. f.write("Hola\n")
  95. f.close()
  96.  
  97. 2) LO MISMO PERO CON WITH (PREFERIDA) NO NECESITO USAR close()
  98. with open("archivo.txt", "x") as f:
  99.    f.write("Hola\n")
  100. """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement