Python: Working with text files

The ability to work with files is an integral part of any programming language. The main operations are: opening, closing, writing, and reading.

File opening. The open() method

To start working with a file, it needs to be opened. For this purpose, the built-in method open() is used.

The general form looks like this:

f = open('< file path (name)>', '< mode'[, encoding]>)

The file path can be either relative or absolute. If we only specify the file name with its extension, it is implied that the file should be in the same folder as the program itself. There are several file opening modes, let's consider them in the form of a table.

File opening modes

Mode Description
'r' Open for reading (default).
'w' Open for writing. If the file exists, its contents are deleted, otherwise a new file is created.
'x' Open for writing, if the file does not exist. Otherwise, an exception is raised.
'a' Open for appending. Information is added to the end of the file. If the file does not exist, it will be created automatically.
'b' Open in binary mode.
't' Open in text mode (default).
'+' Open for reading and writing.

Modes can be combined, for example, 'rb' - reading in binary mode. By default, the mode is 'rt'. The last argument, encoding, is only needed in text mode when reading a file. This argument sets the encoding.

Closing the file. The close() method

After working with a file in a program, it is essential to close it, otherwise, when attempting to modify and save the file with another program, we will receive a message that the file is being used by another application, and the saving will fail. The close() method of the file object automatically closes the file, losing any unsaved information in the process. It is not possible to work with the file (read, write) after this. Python automatically closes the file if the file object it is bound to is assigned to another file.

Code: text file

# will open a file for writing, and if it does not exist, it will create
f = open('out.txt','w')

# will open the file to continue recording 
f = open('out.txt','a')

# close file
f.close()

Writing to a file. The write() method

The write() method writes any string to the open file. It's important to remember that strings in Python can contain binary data, not just text.

The write() method does not add a newline character '\n'. Therefore, if a line break is needed, we need to add the newline character ourselves.

If it's necessary to write non-string data (such as numbers) to a file, it's essential to convert them to strings before writing, otherwise, the interpreter will raise an error.

Syntax of the write() method:

f.write(string)

Here, f is our file object, and string is the string to be written to the file.

Code: Writing to a file

f = open('out.txt', 'w')
f.write('first line\n')
f.write('second line\n')


'''
So that the data appears in the file,
You must close this file in the
program using the close() method
'''
f.close()

You can also output data to a file using the print function if pass it another named parameter file, equal to a link to the open file.

Unlike the write() method, print() does not require the data to be converted to a string type before writing to the file. Also, the print() method automatically adds line breaks.

Code: Writing to a file

f = open('out.txt', 'w')
print('first line', file=f)
f.close()

Reading from a file. The read() method

Method 1

Involves reading the entire file or reading a specified number of bytes. To implement this, the read() method is used. If the parentheses are empty, the entire file is read from the current cursor position. If a number is specified, the specified number of bytes is read.

Example:

Let's say we have a file named input.txt with the following content:

hello

python

There are 2 words in the file, each on a new line. We execute the code:

Code: Read from a file

f = open('input.txt', 'rt')
print(f.read(1))
print(f.read())

# h
# ello
# python

'''
Note that the first time only one character was read,
and the cursor also moved by one character.
During the second read, everything was read
from the cursor position onwards.
'''

Method 2

Line-by-line reading.

To implement line-by-line reading, the readline() method is used. Each time the method is called, a complete line is read, and the pointer moves to the next line. Suppose we have a file named input.txt with the following content:

hello

python

As a result, we will get three lines: the first two are the lines from the file, and the third is an empty line. If the lines have been exhausted, the program will not raise an error. Instead, it will receive empty lines.

Code: Read from a file

f = open('input.txt', 'rt')
print(f.readline())
print(f.readline())
print(f.readline())
f.close()

# hello
#
# python

When displayed on the screen, an unnecessary empty line will appear between our output lines. This is not an error. The problem is that each time print is used, it automatically adds a newline character at the end, while the file also contains newline characters. To fix this situation, we can modify the print statement as follows:

print(s, end='')

Example of outputting two lines without an empty line between them:

Code: Read from a file

f = open('input.txt', 'rt')
print(f.readline(), end='')
print(f.readline())
f.close()


# hello
# python

Method 3

Line-by-line reading using a loop. To implement the third method, a for loop is used. In this method, the read() method is not used. We work with the same file, input.txt.

Code: Read from a file

f = open('input.txt', 'rt')
for s in f:
    print(s, end='')
    
# hello
# python

Method 4

Automatic reading of the entire file, obtaining a list consisting of its lines. We use the same file input.txt with two lines.

Note that the newline character '\n' remains at the end of each element. To remove newline characters from each element, you can use the string processing method rstrip().

Code: Read from a file

f = open('input.txt','rt')
s = f.readlines()
print(s)
f.close()
['privet\n', 'python']

Links: Python documentation


[1] Reading and Writing Files


[2] CSV Files