Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- !/usr/bin/env python
- # -*- coding: utf-8 -*-
- #
- import datetime
- """
- ========= ===============================================================
- 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.
- Python distinguishes between files opened in binary and text modes,
- even when the underlying operating system doesn't. Files opened in
- binary mode (appending 'b' to the mode argument) return contents as
- bytes objects without any decoding. In text mode (the default, or when
- 't' is appended to the mode argument), the contents of the file are
- returned as strings, the bytes having been first decoded using a
- platform-dependent encoding or using the specified encoding if given.
- Funciuones varias sobre archivo:
- 1) read(), readline(), readlines(): leen el contenido del archivo
- (entero, una sola linea, entero linea por linea)
- 2) Existen similares para escritura:
- write(), writeline(), writelines()
- 3) No hay funcion para grabar, se graba automàticamente al leer
- 4) OJO: se debe cerrar (si no se cierra, se puede perder lo grabado)
- close()
- """
- # Script que crea un archivo con temperaturas en ºC y luego otro en ºF
- # obtenido a partir del primero
- # creo el archivo y lo abro en modo escritura
- try:
- f = open("celsius.txt","x")
- except FileExistsError:
- f = open("celsius.txt","a")
- # creo una lista con las líneas para añadir
- datos = ["25.8ºC\n","-14.2ºC\n","8ºC\n","23.4ºC\n","-11.5ºC\n"]
- # agrego los datos al archivo
- #for dato in datos():
- # f.writeline(dato)
- f.write(datetime.datetime.now().strftime("\n%d/%m/%y %H:%M:%S\n"))
- f.writelines(datos)
- # si quisiera imprimir los datos, debo cerrarlo y abrirlo en modo lectura
- f.close()
- f = open("celsius.txt")
- print(f.read())
- f.close()
- # voy a crear el archivo farenheit.txt
- try:
- f = open("farenheit.txt","x")
- except FileExistsError:
- f = open("farenheit.txt","a")
- # Le agrego los datos
- f.write(datetime.datetime.now().strftime("\n%d/%m/%y %H:%M:%S\n"))
- for dato in datos:
- temp,escala = dato.split("º")
- temp = float(temp) * 1.8 + 32
- f.write(f"{temp:.1f}ºF\n")
- f.close()
Add Comment
Please, Sign In to add comment