Introduction
Open and close files
To interact with any type of file, we first need to know a bit about some general functions. open(file, mode='r',...)
is the first, and is used to open a file. Using the parameter mode, we can here choose how the file is to be opened, as well as what we are able to do with it. The following table is taken from docs.python.org.
Character | Meaning |
---|---|
'r' | open for reading (default) |
'w' | open for writing, truncating1 the file first |
'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 |
'b' | binary mode |
't' | text mode (default) |
'+' | open for updating (reading and writing) |
1Means removing the file contents without deleting the file.
It is also important to close the file after we are done with it, which we will show in the following examples. If you are unsure why it is important to close the file, please have a look at this post on Stack Overflow discussing the topic.
As a final note on how to open files, we will open a file not located in our current directory (this refers to the location of the executed python program/file). To do so, we need to use an absolute path, as shown in the example below.
# Open file in current directory fid = open('input.txt', 'r') # Open file in another directory fid = open('C:/Users/input.txt', 'r')
Basic methods for reading and writing to files
This section will display some basic functions for reading and writing to files. To not spend to much space on simple functions, we recommend you to read the documentation by docs.python.org for more details.
Text file [.txt]
Text files are a very common file for storing information, mostly due to their simplicity. Although text files can be interpreted as a general level of description (compared to binary files), or include multiple suffixes for language-specific text files (e.g. .mat and .py), we will in this section only consider the suffix .txt. As seen in the introduction, several methods for working with text files have already been established. However, most of these are not considered effective, especially when introducing large quantitives of data through e.g. matrices.
Using NumPy
To read all about input and output in NumPy, visit the NumPy documentation. As for our example information (introduced at the top of this page), we will first aim to write it to a file. For this, we will be using the function numpy.savetxt(fname, X, fmt='%.18e', delimiter=' ', newline='n', header='', footer='', comments='# ', encoding=None)
. At first, this may seem like a lot of parameters for one simple function, but hopefully, you will soon see it as a pure benefit.
As opposed to numpy.savetxt()
, numpy.loadtxt(fname, dtype=<class 'float'>, comments='#', delimiter=None, converters=None, skiprows=0, usecols=None, ...)
is used to load data from a text file.
Comma separated value file [.csv]
A CSV-file is delimited text file that uses commas (but may also use e.g. semicolons, colons, tab etc;) to separate values. Because CSV-files are written in plain text, they are both easy to manually read, as well as import to a software such as e.g. Excel.
Using csv
csv is a Python module that implements classes to read and write tabular data in csv format.
First, let's import the data as a regular Python list.
The second example will display how you may use csv to import the data to a Python dictionary.
The previous example using the Python dictionary container is usually not applied for the exact type of information we used ((x,y,z)-values). A more commonly used input-file for the case of a dictionary could be:
Using NumPy
Seeing as CSV-files are a specific type of text-files, the functions introduced for regular text-files (np.loadtxt() and np.savetxt()) are also applicable here. The difference between the two is located in the delimiter-parameter and the suffix of the outputted file.
Another function that may be used is numpy.genfromtxt()
. This function is equivalent to numpy.loadtxt() when no data is missing, but very useful if we should have missing data. A simple example displaying one of the unique parameters of np.genfromtxt() is shown below. For further information, please visit the function documentation.