Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- #
- # funciones importantes para manejo de archivos:
- # open(), read(), write(), close()
- """
- ========= ===============================================================
- 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.
- Script que crea un archivo de texto con temp en ºC y otro vacío, luego
- leo el contenido del archivo con temp enºC y las paso a ºF para guardarlas
- en el segundo archivo
- """
- ####### creo el archivo vacío, le guardo datos y lo cierro
- f = open("centigrados.txt","x")
- datos = ['10ºC\n', '13ºC\n', '-15.2ºC\n', '-24.1ºC\n']
- # Método 1: usando write()
- for dato in datos:
- f.write(dato)
- # Método 2: usando writelines()
- # f.writelines(datos)
- f.close()
- ###### creo el segundo archivo
- f = open("farenheit.txt","x")
- # abro el primer archivo para leer los datos
- f2 = open("centigrados.txt")
- temperaturas = f2.readlines()
- # guardo los datos en el segundo archivo (ºF):
- for linea in temperaturas:
- temp, escala = linea.split("º")
- temp = float(temp) * 1.8 + 32
- f.write("{:.1f}ºF\n".format(temp))
- # muestro el contenido de farenheit.txt
- # como esta en modo escritura, tengo que cerrarlo
- # y abrirlo nuevamente en modo lectura:
- f.close()
- f = open("farenheit.txt")
- valores = f.readlines()
- for linea in valores:
- print(linea.strip())
- # cierro los archivos
- f.close()
- f2.close()
Advertisement
RAW Paste Data
Copied
Advertisement