Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- #
- """
- Funciones básicas para trabajar con archivos: open, write, read, close
- Existen funciones especificas: writeline, writelines, readline, readlines
- ========================================================================
- Character Meaning
- --------- ---------------------------------------------------------------
- 'r' open for reading (default)
- 'w' open for writing, truncating the file first
- 'x' create a new file and open it for writing
- 'a' open for writing, appending to the end of the file if it exists
- 'b' binary mode
- 't' text mode (default)
- '+' open a disk file for updating (reading and writing)
- 'U' universal newline mode (deprecated)
- ========= ===============================================================
- The default mode is 'rt' (open for reading text). For binary random
- access, the mode 'w+b' opens and truncates the file to 0 bytes, while
- 'r+b' opens the file without truncation. The 'x' mode implies 'w' and
- raises an `FileExistsError` if the file already exists.
- Python distinguishes between files opened in binary and text modes,
- even when the underlying operating system doesn't. Files opened in
- binary mode (appending 'b' to the mode argument) return contents as
- bytes objects without any decoding. In text mode (the default, or when
- 't' is appended to the mode argument), the contents of the file are
- returned as strings, the bytes having been first decoded using a
- platform-dependent encoding or using the specified encoding if given.
- """
- # Script que crea un archivo vacìo y lo completa con temperaturas en ºC.
- # Luego, lo cierra, lo muestra y le agrega datos. Despuès de esto, crea
- # otro archivo con las temperaturas del archivo anterior pero en ºF
- # creo un archivo vacío
- f = open("centigrados.txt","x")
- datos = ["-10.2ºC\n", "11.2ºC\n","-25.8ºC\n","0ºC\n"]
- # Guardo los datos en el archivo
- # for dato in datos:
- # f.write(dato)
- f.writelines(datos)
- # cierro el archivo
- f.close()
- # lo abro en modo lectura para leer los datos
- f = open("centigrados.txt")
- # imprimo el contenido
- # print(f.readlines())
- for linea in f:
- print(linea.strip())
- f.close()
- # abro para agregar datos
- f = open("centigrados.txt","a")
- f.write("-10ºC\n")
- f.close()
- # creo el archivo en farenheit
- f = open("centigrados.txt")
- datos = f.readlines()
- f.close()
- f = open("farenheit.txt","x")
- for dato in datos:
- temp,escala = dato.split("º")
- temp = float(temp) * 1.8 + 32
- f.write(f"{temp:.1f}ºF\n")
- f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement