Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- Existen 3 funciones básicas para archivos:
- open() -> abre un archivo
- read() -> lee el archivo
- write() -> escribe una línea en el archivo
- Otras:
- readline -> lee una línea del archivo
- readlines -> lee todas las líneas del archivo
- writelines -> escribe todas las líneas en el archivo
- close -> cierra el archivo
- ========= ===============================================================
- 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)
- ========= ===============================================================
- Script que lee un archivo con temperaturas expresadas en ºC y las convierte a ºF. Debe crear
- previamente el archivo con temp. en Celsius y guardar también el archivo en ºF
- """
- # creo el archivo con temp en ºC:
- f = open("centigrados.txt","x")
- # creo una lista con los datos
- datos = ['10ºC\n', '125.2ºC\n', '0.25ºC\n', '14.5ºC\n', '-14.02ºC\n']
- # escribo los datos en el archivo
- f.writelines(datos)
- # cierro el archivo centígrados
- f.close()
- # creo el archivo con temp en ºF:
- f = open("farenheit.txt","x")
- # convierto línea a línea y escribo en "farenheit.txt"
- for linea in datos:
- temp, escala = linea.split("º")
- temp = float(temp) * 1.8 + 32
- f.write("{:.1f}ºF\n".format(temp))
- # cierro el archivo
- f.close()
- ## adicional: supongamos que tengo que agregar un valor (15ºC) a "centigrados.txt"
- f = open("centigrados.txt","a")
- f.write("15ºC\n")
- f.close()
- temp = 15 * 1.8 + 32
- f = open("farenheit.txt","a")
- f.write("{:.1f}ºF\n".format(temp))
- f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement