EniDev

Guías sobre temas de bases de datos y programación


Project maintained by Hosted on GitHub Pages — Theme by mattgraham

badge python

open()

La función open() se usa para abrir archivos almacenados. Devuelve el contenido del archivo como objeto python.

Sintaxis

open(file_name, mode)

Parámetros

Ejemplos

Para crear un archivo de texto llamado “archivo.txt”.

file = open("archivo.txt", "x")

Leer y escribir un archivo

file = open("archivo.txt", "w")
file.write("Aprendiendo a escribir en archivos con Python.")
file.close()

# leemos el archivo
file = open("archivo.txt")
print(file.read())

Agregar contenido al archivo

file = open("archivo.txt", "a")
file.write("Añadiendo más texto en el archivo")
file.close()

# leemos el archivo
file = open("archivo.txt")
print(file.read())