Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- #
- """
- open(), write(), read(), close(), writelines() 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)
- ========= ===============================================================
- """
- # creo un archivo vacío 'celsius.txt'
- f = open("celsius.txt","x")
- # preparo los datos
- datos1 = ['10ºC\n', '22ºC\n', '-14.258ºC\n', '11ºC\n']
- datos2 = ['8ºC\n', '12.5ºC\n', '102ºC\n', '-0.2581ºC\n']
- # guardo los datos en el archivo
- # forma 1:
- f.writelines(datos1)
- # forma 2:
- for dato in datos2:
- f.write(dato)
- # cierro el archivo
- f.close()
- # abro un archivo para guardar los datos en ºF
- f = open("farenheit.txt", "x")
- # abro el archivo "celsius.txt" para leer sus datos y pasarlos a ºF
- f2 = open("celsius.txt")
- contenido = f2.readlines()
- # ya leí "celsius.txt", lo cierro
- f2.close()
- # escribo el archivo "farenheit.txt"
- for fila in contenido:
- temp, escala = fila.split("º")
- temp = float(temp) * 1.8 + 32
- f.write("{:.1f}ºF\n".format(temp))
- f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement