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
- '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.
- open() --> abre un archivo (si no existe lo puede crear)
- close() --> cierra un archivo
- read() --> lee el contenido de un archivo
- write() --> escribe el contenido en un archivo
- readline, readlines, writeline, writelines: manejan archivos como si
- fueran listas de renglones
- ej: numeros.txt
- 1
- 2
- 3
- readline() --> lee [1]
- readlines() --> lee [1,2,3]
- """
- # Script que crea un archivo de texto con temperaturas en ºC y luego
- # crea otro equivalente en ºF
- # Creo un archivo vacío celsius.txt
- try:
- f = open("celsius.txt", "x")
- except FileExistsError:
- f = open("celsius.txt", "w")
- # creo una lista de datos para guardar en celsius.txt
- datos = ["10ºC\n","-14.2ºC\n","0.1ºC\n","-3ºC\n","11ºC\n"]
- # agrego los datos al archivo:
- #for dato in datos:
- # f.write(dato)
- #f.close()
- f.writelines(datos)
- f.close()
- # lo abro para leer e imprimirlo
- f = open("celsius.txt")
- print(f.read())
- # Supongamos que me hubiera olvidado un dato, y se lo quiero añadir:
- f.close()
- f = open("celsius.txt","a")
- f.write("100ºC\n")
- f.close()
- # Debo leer nuevamente el archivo celsius.txt para crear farenheit.txt
- f = open("celsius.txt")
- datos = f.readlines()
- f.close()
- # creo farenheit.txt
- f = open("farenheit.txt","x")
- # convierto a ºF las temperaturas y las guardo
- for dato in datos:
- temp, escala = dato.split("º")
- temp = float(temp) * 1.8 + 32
- f.write(f"{temp:.1f}ºF\n")
- f.close()
- """
- 1) Creo un archivo y le escribo "Hola"
- f = open("archivo.txt", "x")
- f.write("Hola\n")
- f.close()
- 2) LO MISMO PERO CON WITH (PREFERIDA) NO NECESITO USAR close()
- with open("archivo.txt", "x") as f:
- f.write("Hola\n")
- """
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement