Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- """
- Script que genera un archivo con temperaturas en ºC. Luego, genera otro
- similar a partir del primero pero con las temperaturas en ºF
- funciones: open,close,read,write,readline,writeline,readlines,writelines
- ========= ===============================================================
- 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)
- ========= ===============================================================
- 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.
- """
- # creo un archivo nuevo "celsius.txt"
- try:
- f = open("celsius.txt", "x")
- except FileExistsError:
- f = open("celsius.txt","a")
- # creo una lista de datos
- datos = ["10ºC\n","12.5ºC\n","-0.58ºC\n", "215ºC\n", "-25.8ºC\n"]
- # escribo los datos en el archivo
- f.writelines(datos)
- # cierro el archivo
- f.close()
- # abro de nuevo el archivo para agregarle datos. Esta forma de apertura
- # LO CIERRA AUTOMATICAMENTE
- with open("celsius.txt","a") as f:
- f.write("11.5ºC\n")
- # imprimo el contenido
- with open("celsius.txt") as f:
- print(f.read())
- # creo el archivo "farenheit.txt"
- try:
- f = open("farenheit.txt", "x")
- except FileExistsError:
- f = open("farenheit.txt","a")
- # abro celsius.txt para leer sus datos
- with open("celsius.txt") as f1:
- datos = f1.readlines()
- # lleno el archivo farenheit con los datos
- for dato in datos:
- temp, _ = dato.split("º")
- temp = float(temp) * 1.8 + 32
- f.write(f"{temp:.1f}ºF\n")
- # cierro el archivo farenheit
- f.close()
- # imprimo el contenido de farenheit
- with open("farenheit.txt") as f:
- print(f.read())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement