Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- """
- ========= ===============================================================
- 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 manejar archivos: open(), read(), write(), close()
- readlines(), writelines(), ....
- """
- # Script que crea un archivo con temperaturas en ºC, luego crea otro
- # archivo y lo llena con el equivalente del primero pero expresado en
- # ºF
- # creo un archivo vacío "celsius.txt"
- f = open("celsius.txt", "x")
- # creo una lista con los datos a guardar en el archivo
- datos = ["20ºC\n", "11ºC\n", "15.2ºC\n", "-10.7ºC\n"]
- # escribo los datos en el archivo
- # modo 1: con write()
- #for dato in datos:
- # f.write(dato)
- # modo 2: con writelines()
- f.writelines(datos)
- # cierro el archivo
- f.close()
- # supongamos que nos olvidamos de un dato, hay que agregarlo
- f = open("celsius.txt", "a")
- f.write("100ºC\n")
- # creo el archivo "farenheit.txt"
- f2 = open("farenheit.txt", "x")
- # tengo que ller los valores de "celsius.txt" y convertirlos a ºF
- # tengo que cerrar el archivo y abrirlo en modo lectura
- f.close()
- f = open("celsius.txt")
- # creo una lista leyendo todos los valores de temp de "celsius.txt"
- datos = f.readlines()
- # leo uno por uno cada dato lo convierto y lo escribo
- # en "farenheit.txt"
- for dato in datos:
- temp, escala = dato.split("º")
- temp = float(temp) * 1.8 + 32
- f2.write("{:.1f}ºF\n".format(temp))
- # cierro todos los archivos
- f.close()
- f2.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement