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 (MODO APPENDING)
- '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)
- ========= ===============================================================
- The default mode is 'rt' (open for reading text). For binary random
- access, the mode 'w+b' opens and truncates the file to 0 bytes, while
- 'r+b' opens the file without truncation. The 'x' mode implies 'w' and
- raises an `FileExistsError` if the file already exists.
- """
- """
- Script que crea un archivo con temp en ºC y luego crea otro con temp
- en ºF a partir del primero
- """
- # para trabajar con archivos: open(), read() write() close()
- # menos usadas: writelines(), readlines(), etc
- # abro un archivo de texto y le agrego datos
- try:
- f = open("celsius.txt", "x")
- except FileExistsError:
- f = open('celsius.txt','a')
- datos = ["10ºC\n", "-25.3ºC\n", "66ºC\n", "12ºC\n"]
- # for dato in datos:
- # f.write(dato)
- f.writelines(datos)
- # agrego un dato más:()
- f.write("100ºC\n")
- # creo el archivo farenheit
- f2 = open("farenheit.txt", "x")
- # leo los valores de celsius.txt. Como está en modo escritura, lo cierro
- # y lo abro en modo lectura
- f.close()
- f = open("celsius.txt")
- # imprimo los valores
- print(f.read())
- # cuando leo, se vacía la variable f. Para leer nuevamente, debo cerrar
- # y abrir
- f.close()
- f = open("celsius.txt")
- # leo los valores y los guardo en una lista
- valores = f.readlines()
- # convierto cada linea de ºC a ºF y la grabo en farenheit.txt
- for valor in valores:
- temp, _ = valor.split("º")
- temp = float(temp) * 1.8 + 32
- f2.write(f"{temp:.1f}ºF\n")
- # cierro los archivos
- f.close()
- f2.close()
- """ USO DE WITH
- with open("saludo.txt","x") as f:
- f.write("Hola")
- with open("saludo.txt") as f:
- print(f.read())
- """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement