Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # -*- coding: utf-8 -*-
- #
- """
- '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)
- """
- # creo un archivo para guardar datos
- f = open("datos.txt", "x")
- # creo una lista con las filas para guardar
- lineas = [
- "Listado de nombres\n",
- "------------------\n",
- "Alejandro\n",
- "Ana\n",
- "Luisa\n",
- "Mario\n"
- ]
- # guardo las filas en datos.txt
- for linea in lineas:
- f.write(linea)
- # cierro el archivo
- f.close()
- # abro el archivo para agregar un dato
- f = open("datos.txt", "a")
- # agrego el dato
- f.write("Pepito\n")
- # cierro el archivo
- f.close()
- # abro el archivo para ver su contenido
- f = open("datos.txt")
- # leo el contenido y lo imprimo
- lineas = f.readlines()
- for linea in lineas:
- print(linea,end="")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement