Professional Documents
Culture Documents
Lectura y Escritura de Archivos: Computación Numérica 2589343 Algoritmos y Programación 2568364
Lectura y Escritura de Archivos: Computación Numérica 2589343 Algoritmos y Programación 2568364
archivos
Computación Numérica 2589343
Algoritmos y Programación 2568364
Departamento de Ingeniería Eléctrica
Facultad de Ingeniería
2020-1
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
https://docs.python.org/3/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()
●
readline(): reads a single line from file with
newline at the end.
●
read(): returns a string with the entire file.
●
readlines(): returns a list containing all the
lines in the file.