teslariu

archivos

Aug 13th, 2022
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.92 KB | None | 0 0
  1. !/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. import datetime
  5. """
  6. ========= ===============================================================
  7.    Character Meaning
  8.    --------- ---------------------------------------------------------------
  9.    'r'       open for reading (default)
  10.    'w'       open for writing, truncating the file first
  11.    'x'       create a new file and open it for writing
  12.    'a'       open for writing, appending to the end of the file if it exists
  13.    'b'       binary mode
  14.    't'       text mode (default)
  15.    '+'       open a disk file for updating (reading and writing)
  16.    'U'       universal newline mode (deprecated)
  17.    ========= ===============================================================
  18.  
  19.    The default mode is 'rt' (open for reading text). For binary random
  20.    access, the mode 'w+b' opens and truncates the file to 0 bytes, while
  21.    'r+b' opens the file without truncation. The 'x' mode implies 'w' and
  22.    raises an `FileExistsError` if the file already exists.
  23.  
  24.    Python distinguishes between files opened in binary and text modes,
  25.    even when the underlying operating system doesn't. Files opened in
  26.    binary mode (appending 'b' to the mode argument) return contents as
  27.    bytes objects without any decoding. In text mode (the default, or when
  28.    't' is appended to the mode argument), the contents of the file are
  29.    returned as strings, the bytes having been first decoded using a
  30.    platform-dependent encoding or using the specified encoding if given.
  31.  
  32. Funciuones varias sobre archivo:
  33. 1) read(), readline(), readlines(): leen el contenido del archivo
  34. (entero, una sola linea, entero linea por linea)
  35.  
  36. 2) Existen similares para escritura:
  37. write(), writeline(), writelines()
  38.  
  39. 3) No hay funcion para grabar, se graba automàticamente al leer
  40.  
  41. 4) OJO: se debe cerrar (si no se cierra, se puede perder lo grabado)
  42. close()
  43.  
  44. """
  45.  
  46. # Script que crea un archivo con temperaturas en ºC y luego otro en ºF
  47. # obtenido a partir del primero
  48.  
  49. # creo el archivo y lo abro en modo escritura
  50. try:
  51.     f = open("celsius.txt","x")
  52. except FileExistsError:
  53.     f = open("celsius.txt","a")
  54.  
  55. # creo una lista con las líneas para añadir
  56. datos = ["25.8ºC\n","-14.2ºC\n","8ºC\n","23.4ºC\n","-11.5ºC\n"]
  57.  
  58. # agrego los datos al archivo
  59. #for dato in datos():
  60. #    f.writeline(dato)
  61. f.write(datetime.datetime.now().strftime("\n%d/%m/%y %H:%M:%S\n"))
  62. f.writelines(datos)
  63.  
  64. # si quisiera imprimir los datos, debo cerrarlo y abrirlo en modo lectura
  65. f.close()
  66. f = open("celsius.txt")
  67. print(f.read())
  68. f.close()
  69.  
  70. # voy a crear el archivo farenheit.txt
  71. try:
  72.     f = open("farenheit.txt","x")
  73. except FileExistsError:
  74.     f = open("farenheit.txt","a")
  75.  
  76. # Le agrego los datos
  77. f.write(datetime.datetime.now().strftime("\n%d/%m/%y %H:%M:%S\n"))
  78. for dato in datos:
  79.     temp,escala = dato.split("º")
  80.     temp = float(temp) * 1.8 + 32
  81.     f.write(f"{temp:.1f}ºF\n")
  82. f.close()
  83.  
Add Comment
Please, Sign In to add comment