Professional Documents
Culture Documents
Files Files
Lectura de archivos Algoritmos y Programación 2
Read and write files
open: Open file and return a corresponding file
object.
Character Meaning
'x' open for exclusive creation, failing if the file already exists
'a' open for writing, appending to the end of the file if it exists
https://docs.python.org/3.5/library/functions.html#open
file
file == open('names.txt',
open('names.txt', 'a')
'a') Imprime:
file.write('Clara\n')
file.write('Clara\n') Juan
file.write('Julia\n')
file.write('Julia\n') Ana
file.close()
file.close() Clara
file
file == open('names.txt',
open('names.txt', 'r')
'r') Julia
for
for line
line in
in file:
file:
print(line[:-1])
print(line[:-1])
file.close()
file.close()
file
file == open('names.txt','r+')
open('names.txt','r+') Imprime:
file.write('Ana')
file.write('Ana') Anares
file.close()
file.close() Pedro
file
file == open('names.txt','r')
open('names.txt','r')
for
for line
line in
in file:
file:
print(line[:-1])
print(line[:-1])
file.close()
file.close()