Python File I/O Tutorial: Read, Write, Append

Python File I/O Tutorial: Read, Write, Append

Python is a language with batteries included. It comes with a large collection of standard modules that you can use as the basis of your programs and for everyday usages.

One of them is File I/O Module. In this post, you will learn Python file operations from soup to nuts.

Shall we start πŸ™‚

Call The Open Function

For any file operation the first step is calling the Open function.

It is the most important function for file operations.

f = open("sample.txt","r")

When you open a function it returns a file object. File Object contains useful functions to do operations on intended files. 

Two parameters are crucial:

File: File path (absolute or relative)

Modes: Mode parameter is an optional string that specifies the mode in which the file is opened. The default mode is r, which means read mode.

By mode you reveal your intention: Reading text, reading binary, writing, appending etc.

File Modes

Character Meaning
‘r’ open file for reading (default)
‘w’ open file for writing: Truncates the file first
‘a’ open file for writing, appending to the end of the file if it exists
‘b’ open in binary mode
‘t’ text mode (default)
‘+’ open a disk file for updating (reading and writing)

Some File Mode characters could be used to together.

For example ‘rb’ means, open file in read and binary mode.

python file i/o tutorial
beginner friendly file i/o tutorial

File Mode Examples:

  • ‘wb’: Open in write and binary mode
  • ‘w+’: Open in write and text mode, but you can also read the file
  • ‘rb+’: Open the file in read, binary mode, but you can also write to the file.

Read Text Files

Text files are human readable files. Text files are files that contain unformatted text files. It contains letters, numbers, symbols.

They are non-executable, unlike binary files. In windows, text files have .txt extension as a convention, but it is not necessary.

Open a sample file (sample.txt) and print the file content to the console.

f = open("sample.txt",mode="r")
print(f.read())

We are creating a file handle (f) using the open function, with file path parameter and read mode parameter.

If you try to open a file which does not exist in ‘r’ mode, Python raises a FileNotFoundException.

read function without size parameter reads all the file at once.

sample.txt

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in
voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident,
sunt in culpa qui officia deserunt mollit anim id est laborum

print(f.read()) outputs the file content verbatim:

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut
labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation
ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in
voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident,
sunt in culpa qui officia deserunt mollit anim id est laborum

print(read(4)); read function with size (byte) parameter, reads only 4 bytes, so the output is:

lorem

Read Text Files line by line

Using readlines function of the file object, we can read the file line by line.

f = open("sample.txt",mode="r")

lines = f.readlines()

for x in lines:
    print(x)

output:

line 1
line 2
line 3

Read Text Files line by line (Memory efficient way)

The example before uses the readlines method. It will load all lines to the memory. So if the file is huge it could be a problem.

The code below is the memory efficient way:

with open('workfile') as f:
    for line in f:
        process(line)

with is a python keyword which ensures that clean-up code, in our example f.close(), is implicitly executed.

Writing To A File

We call the open function with write (‘w’) mode. Write mode, overwrites the file content. If the file does not exists it creates the file.

Sample:

f = open("out.txt","w")
f.write("line 1")

out.txt:

line 1

Append To A File

To append to a file we open the file with the append mode (‘a’) parameter.

f = open("out.txt","a")
f.write("\nline 2")

Append mode is different from write mode, because it does not overwrite the content. As the name implies, it appends at the end of the file.

The output would be:

out.txt

line 1
line 2

How to Read A Binary File

Binary files are files that contain binary data. Binary data are not human readable. It could only be understood by computers.

Executable files (exe) and image files are samples of binary files. We are appending ‘b’ to the ‘r’ file mode parameters. Which denotes that the file is a binary file. 

f = open("simpleimage.png","rb")
print(f.read())

Code outputs some binary data:

b'\x89PNG\r\n\x1a\n\x00\x00\..........

Always Be Closing (Like in the Movie πŸ™‚ )

You should always close the file object after you are finished with the file. Depending on your python interpreter your modifications to the file are not flushed to the disk if you are not close the file.

As a best practice, you should close your file after you are done with the file.

f = open('sample.txt','r+')

print(f.read)

f.close()

But calling close() statement like in our example is not the ideal way. Before the close statement, unexpected things (exceptions) could occur. So our file would not be closed.

You can wrap your code in a try finally block:

f = open("workfile", "w")
try:
   f.write('Hello World!')
finally:
   f.close()

The best way of closing and opening files is with the β€œwith” statement, as in the following:

with open('mysamplefile.txt') as f:
    for line in f:
        print(line)

After the with statement python automatically closes the file.

Useful Functions: Seek, Tell, Truncate

seek

When we are opening a file, it returns a pointer which points to the beginning of the file. When we are reading a character, the position moves further by one. 

So any file read, write operation changes the file position. But if we want to move the file position to the desired place, we can use the seek function.

fileObject.seek(offset[, whence]) 

offset is the number of how many bytes to move the file position. whence is the starting point for the file position.

We can use some useful constansts for whence parameter beside a number (0, 1, 2).

  • os.SEEK_SET – beginning of the file. Absolute position. It is equal to 0. 
  • os.SEEK_CUR – current position. Relative to the current file position.  It is equal to 1
  • os.SEEK_END – end of the file. Relative to the end of the file. It is equal to 2.
f = open('samplefile.txt','r+')
f.write('1234abcdefg')
f.seek(0)                            # go to at the start
print(f.read(1))                     # outputs 1
print(f.read(1))                     # outputs 2
f.seek(8)                            # go to the file position at 8th byte
print(f.read(1))                     # outputs e

tell

tell is another useful function. It returns the current file position:

f = open('samplefile.txt','w+')
f.write('1234abcdefg')
f.seek(0)                             # go to at the start
print('file position:', f.tell())     # file position: 0
print(f.read(2))                      # 12
print(f.read(1))                      # 3
print(f.read(3))                      # 4ab
print('file position:', f.tell())     # file position: 6

truncate

truncate clears the content of the file. 

truncate([size])

If it is called with size parameter, it resizes the file to the given number of bytes. If I say truncate(10) it resizes the file to 10 bytes.

Search and replace text in a file with Python File I/O

With a single with block, you can search and replace your text, thanks to python’s versatility.

with open('samplefile.txt','r+') as f:
    filedata = f.read()
    filedata = filedata.replace('abc','xyz')   
    f.truncate(0)
    f.write(filedata)

f.read reads all data. We are replacing our text (‘abc’) with ‘xyz’ in the filedata object. To overwrite our file content, I’m using the truncate method.

Saving your Python Object To A File

For development purposes, you can save your python objects to a file also. It is also called serializing.

There are multiple options that you can choose. The recommended and the secure way and the not so recommended way:)

Save your Object to a Json File (Recommended)

Best file format to save your object is json file format. Json is a universally known file format if so. It could be used from other programming languages and environments.

import json
x = [2, 3, 4]
with open('workfile.json','w') as f:
    json.dump(x, f)  # serializing

json.dump writes x to workfile.json file.

To decode from a file object (reverse), we are using load function:

import json
with open('workfile.json','r') as f:
    x = json.load(f)  # deserializing
    print(x)

Save your Object to a Binary File

To save your object to a file, you can use the pickle module. dump method saves the object in binary mode.

import pickle
x = [5, 6, 8]
f = open('workfile','wb')   # open the file for writing
pickle.dump(a,fileObject)   # serializing

deserializing:

x = pickle.load(f)  # deserializing

It couldn’t be used from other programming environments. And it is also not secure. 
Using a pickle file object coming from an untrusted source could be dangerous.

SUMMARY

Any file I/O operation starts with calling open function. It takes a file path parameter and mode parameter.

open function returns a file object. File object contains, read, write, append and many other functions to do file I/O.

In this post you are practically learn everything about file operations. Congratulations.

Leave a Comment