Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- """
- parte de help(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)
- ========= ===============================================================
- Script que lee un archivo con temperaturas expresadas en ºC, las
- convierte en ºF y las guarda en otro archivo
- """
- # creo un archivo vacío 'celsius.txt'
- try:
- f = open("celsius.txt", "x")
- except FileExistsError:
- f = open("celsius.txt", "a")
- # creo una lista con los renglones a escribir
- datos = ["23.3ºC\n", "-25.6ºC\n", "11.5ºC\n", "0ºC\n"]
- # escribo los datos en el archivo
- """
- Una forma, escribiendo dato por dato con un for
- for dato in datos:
- f.write(dato)
- """
- f.writelines(datos)
- # cierro el archivo
- f.close()
- # creo un archivo vacío 'farenheit.txt'
- try:
- f = open("farenheit.txt", "x")
- except FileExistsError:
- f = open("farenheit.txt", "a")
- # leo un dato en ºC , lo paso a ºF y lo agrego al archivo "farenheit.txt"
- for dato in datos:
- temp, escala = dato.strip().split("º")
- temp = float(temp) * 1.8 + 32
- f.write(f"{temp:.1f}ºF\n")
- # cierro el archivo
- f.close()
- # Abro celsius, borro el contenido y le escribo un valor
- # f = open("celsius.txt", "w")
- # f.write("BORRADO\n")
- # f.close() # debe usarse SI o SI
- # lo mismo pero con "with" (no necesita close(), se cierra solo)
- with open("celsius.txt", "w") as f:
- f.write("BORRADO\n")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement