There are a lot of specialized tools to open specialized files (images, xml, csv, hdf5, netcdf, etc.). Here we focus on the low-level general method to open files.
open
built-in function and file object¶file = open('../pyfiles/helloworld.py')
txt = file.read()
file.close()
print(txt)
print('Hello world') name = 'Pierre' print('My name is ' + name)
But what if something weird happens when the file is open (e.g. a division by O) ?
-> Exception is raised that could be catch and run some code that is not aware of the opening file.
-> The file remains open
with
keyword¶For such objects that need to be closed, it is a good practice to use the keyword with
(THIS IS MUCH BETTER than using the close function, USE with
!). Like this, we are sure that the file will be closed even if there is an error:
with open('../pyfiles/helloworld.py') as file:
txt = file.read()
print(txt)
print('Hello world') name = 'Pierre' print('My name is ' + name)
with open('../pyfiles/helloworld.py') as file:
for line in file:
print(f'line ?: ' + line.strip())
line ?: line ?: print('Hello world') line ?: line ?: name = 'Pierre' line ?: print('My name is ' + name)
And now using enumerate
to get the index of the line:
with open('../pyfiles/helloworld.py') as file:
for i, line in enumerate(file):
print(f'line {i:2d}: {line.strip()}')
line 0: line 1: print('Hello world') line 2: line 3: name = 'Pierre' line 4: print('My name is ' + name)
# write data in a file
with open('../TP/zoo.txt', 'w') as file_zoo:
file_zoo.write('sam;cat;2\n')
file_zoo.write('liloo;lion;2\n')
with open('../TP/zoo.txt', 'a') as file_zoo:
file_zoo.write('peter;panda;5\n')
with open('../TP/zoo.txt') as file_zoo:
print(file_zoo.read())
sam;cat;2 liloo;lion;2 peter;panda;5
with open('../TP/zoo.txt', 'r') as file_zoo:
print(file_zoo.readline())
print(file_zoo.read())
sam;cat;2 liloo;lion;2 peter;panda;5
Remark: difference write
and print
.
Until now, we have only written text files. It can of course be much more efficient to use binary format.
with open('/tmp/test', 'wb') as file:
file.write(b'a')
Remarks:
In practice, saving data in binary file is most of the time a bad idea. There are much better solutions to do this (see for example h5py and h5netcdf).
There are Python libraries to read and process many types for files (csv, xml, json, images, tabulars, etc.).