Read / write files#
Built-in function open() and context manager with
Read / write files#
There are a lot of specialized tools to open specific file types (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("../common/examples/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 while the file is open (e.g. a division by 0)?
-> Exception is raised that could be caught and run some code that is not aware of the file being open.
-> The file remains open.
Context: with keyword#
For such objects that need to be closed, it is a good practice to use the keyword with.
Like this, we are sure that the file will be closed even if there is an error:
with open("../common/examples/helloworld.py") as file:
txt = file.read()
print(txt)
print("Hello world")
name = "Pierre"
print("My name is " + name)
Important
This is much better than using the close function: use with!
Loop over lines#
with open("../common/examples/helloworld.py") as file:
for line in file:
print(f"line ?: " + line.strip())
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("../common/examples/helloworld.py") as file:
for i, line in enumerate(file):
print(f"line {i:2d}: {line.strip()}")
line 0: print("Hello world")
line 1:
line 2: name = "Pierre"
line 3: print("My name is " + name)
Options of the built-in function open (read, write, append)#
# write data in a file
with open("/tmp/zoo.txt", "w") as file_zoo:
file_zoo.write("sam;cat;2\n")
file_zoo.write("liloo;lion;2\n")
with open("/tmp/zoo.txt", "a") as file_zoo:
file_zoo.write("peter;panda;5\n")
with open("/tmp/zoo.txt") as file_zoo:
print(file_zoo.read())
sam;cat;2
liloo;lion;2
peter;panda;5
with open("/tmp/zoo.txt", "r") as file_zoo:
print(file_zoo.readline())
print(file_zoo.read())
sam;cat;2
liloo;lion;2
peter;panda;5
Difference between write and print
write writes the raw string in the file: as long as no carriage return (\n) is added, write will write on the same line.
print prints the string to standard output and adds a carriage return at the end.
This is why sam;cat;2 is followed by two carriage returns: one from the line (raw: "sam;cat;2\n") and one added by print.
Options of the built-in function open (binary file)#
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.).