Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- """
- Ayuda de open()
- ========= ===============================================================
- 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)
- ========= ===============================================================
- ## funciones para trabajar con archivos
- open() --> abre un archivo
- read() --> lee el contenido de un archivo (readline, readlines)
- write() --> escribe contenido en un archivo (writeline, writelines)
- close() --> cerrar archivos
- """
- # 1) Crear un archivo "celsius.txt" con valores de temperaturas en ºC
- # 2) Abrir el archivo anterior, añadirle datos e imprimir su contenido
- # 3) Crear un archivo "farenheit.txt" y completarlo con los valores de
- # celsius.txt convertidos a ºF
- # Creo un archivo "celsius.txt". La función open() abre un archivo o lo crea
- f = open("celsius.txt","x")
- # Creo una lista con los renglones del archivo:
- datos = ["25.8ºC\n", "-11.51ºC\n", "0.22ºC\n", "34.8ºC\n", "14.08ºC\n", "-1ºC\n",]
- # añado los datos al archivo
- # for dato in datos():
- # f.write(dato)
- f.writelines(datos)
- # cierro el archivo
- f.close()
- # abro nuevamente el archivo para agregarle datos
- f = open("celsius.txt","a")
- datos = ["100.5ºC\n", "9.08ºC\n"]
- f.writelines(datos)
- # imprimo los datos. Primero debo cerrar y luego abrir en modo lectura
- # para poder imprimir
- f.close()
- f = open("celsius.txt")
- print(f.read())
- # vuelco el contenido del archivo "celsius.txt" a una variable
- # en la línea 53, al imprimir f se queda sin datos. Debo leerlos nuevamente
- f.close()
- f = open("celsius.txt")
- datos = f.readlines()
- f.close()
- # creo el archivo farenheit.txt
- f = open("farenheit.txt","x")
- # convierto los datos a ºF y los escribo en el archivo correspondiente
- for dato in datos:
- temp, escala = dato.split("º")
- temp = float(temp) * 1.8 + 32
- f.write(f"{temp:.1f}ºF\n")
- # cierro el archivo
- f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement