Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- """
- funciones importantes: open(), write(), writelines(), read()
- readlines(), 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.
- """
- # Creo un archivo de texto vacío "centigrados.txt"
- f = open("centigrados.txt", "x")
- # creo una lista de datos para agregarle al archivo
- datos = ["23ºC\n", "-14ºC\n", "12ºC\n", "11ºC\n"]
- # guardo los datos:
- # manera 1:
- # for dato in datos:
- # f.write(dato)
- # manera 2
- f.writelines(datos)
- # cierro el archivo
- f.close()
- # supongamos que lo quiero abrir para agregarle un dato -0.25ºC
- f = open("centigrados.txt","a")
- # le agrego un dato
- f.write("-0.25ºC")
- # creo otro archivo "farenheit.txt"
- f2 = open("farenheit.txt", "x")
- # quiero leer los datos de "centigrados.txt" y pasarlos a "farenheit.txt"
- # debo cerrar centigrados.txt y abrirlo en modo lectura
- f.close()
- f = open("centigrados.txt")
- # leo los datos de centigrados.txt
- datos = f.readlines()
- # transformo cada lina a farenheit y la escribo en el archivo correspondiente
- for dato in datos:
- temp, escala = dato.split("º")
- temp = float(temp) * 1.8 + 32
- f2.write("{:.1f}ºF\n".format(temp))
- # cierro todo
- f.close()
- f2.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement