Further information.
Contents
Further information.#
What is a context manager#
Instead of using the approach shown in How to write to a file it is possible to write to a file using the following:
f = open("squares.csv", "w")
for n in range(1, 101):
f.write(f"{n}, {n ** 2}\n")
f.close()
However, when doing so it is important to close
the file. Without this the
file remains inaccessible for other purposes.
Thus, it is preferred to use:
with open("squares.csv", "w") as f:
for n in range(1, 101):
f.write(f"{n}, {n ** 2}\n")
In this case, the indented block after with open("squares.csv", "w") as f:
is
the context within which the file f
is open. It will be closed outside of that
indented block.
This is an example of a context manager. There are other examples of this in
Python such as try
and except
loops.
Working with files with numpy
#
It is possible to write a numpy
array to a file using the savetxt
function. For
example:
import numpy as np
A = np.array(
(
(1, 5, 3),
(8, 1, 2),
(3, 5, 1),
)
)
np.savetxt("array.txt", A)
It is possible read a file in to a numpy array using the loadtxt
function:
A = np.loadtxt("array.txt")
A
array([[1., 5., 3.],
[8., 1., 2.],
[3., 5., 1.]])
Working with files with pathlib#
The pathlib
library is a powerful tool for reading, writing and manipulating
files.
The documentation for pathlib
is available here: docs.python.org/3/library/pathlib.html