Python file handling: How to write and read files in python



Introduction (python file handling)

The importance of python file handling; in the area of network automation, system administration, data science, or machine learning, reading and writing a file is very common. In order to read or write a file, we need not have to import any module. It is handled natively by a python interpreter.

Working of open() function

Returns a file object and is most commonly used with two arguments:

Syntax:

# open(filename, mode)
>>>f = open('workfile', 'r')

The first argument is a string containing the filename. You should specify the file path and file extension. It is recommended to keep all your files in the same folder in which your script is saved.

The second argument is another string containing a few characters describing the way in which the file will be used. The available mode is:

  • ‘r’       -open for reading (default)
  • ‘w’      -open for writing, truncating the file first
  • ‘x’       -create a new file and open it for writing
  • ‘a’      -open for writing, appending to the end of the file if it exists
  • ‘b’      -binary mode
  • ‘t’       -text mode (default)
  • ‘r+’    -open a disk file for updating (reading and writing). The mode argument is optional; ‘r’ will be assumed if it’s omitted.

Python distinguishes between files opened in binary and text modes, even when the underlying operating system doesn’t. Files opened in binary mode (appending ‘b’ to the mode argument) return contents as bytes objects without any decoding. Binary files are images, pdf, executable files, etc…….

In the text mode (the default, or when ‘t’ is appended to the mode argument), the contents of the file are returned as strings, the bytes having been first decoded using a platform-dependent encoding or using the specified encoding if given.

Once you finish reading a file, then you should call f.close() to close the file and immediately free up any system resources used by it. If you don’t explicitly close a file, Python’s garbage collector will eventually destroy the object and close the open file for you, but the file may stay open for a while. Another risk is that different Python implementations will do this clean-up at different times.

Example:

f = open('about.txt','rt')
content = f.read()
print(content)
print(f.closed)
f.close()
print(f.closed)

Output:

Hello,
We are learning how to read and write files
False
True

In the above example, the mode is ‘rt’ which means open for reading text and it is a default mode. The file about.txt is a text file containing some text (Hello, We are learning how to read and write files) saved in the same directory or folder. read() method is used to open a file to read. Explained more in a later topic under methods of file objects.

f.closed is used to check the status of the file. If the file is not closed it returns False otherwise True.



Importance of with keyword in file handling

It is a good practice to use with keywords when dealing with file objects. The advantage is that the file is properly closed after its suite finishes, even if an exception is raised at some point.

If you’re not using the with keyword, then you should call f.close() to close the file and immediately free up any system resources used by it.

Example:

with open('about.txt', 'rt') as f:
    content = f.read()
    print(content)
print(f.closed)  # Checks whether file is closed or not.

Output:

Hello,
We are learning how to read and write files
True


Methods of File objects

Six available methods of file objects are discussed below with an example.

1. f.read(size)

To read a file’s contents, call f.read(size), which reads some quantity of data and returns it as a string (in text mode) or bytes object (in binary mode). size is an optional numeric argument. When the size is omitted or negative, the entire contents of the file will be read and returned; If the end of the file has been reached, f.read() will return an empty string (”).

Example:

>>>f.read(-1) # Entire contents of the file will be read and returned
>>>f.read(5)  # Only five characters will be read and returned
>>>f.read()   # Entire contents of the file will be read and returned
2. f.readline()

f.readline() reads a single line from the file. If it returns an empty string, it means that end of the file has reached.

Example 1:

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

Output:

Hello,

Example 2:

f = open('about.txt', 'rt')
content = f.readline()   # Reads first line
print(content)
content = f.readline()   # Reads second line
print(content)
f.close()

Output:

Hello,

We are learning how to read and write files
3. f.readlines()

Reads all the lines of the files and returns as a list. At the end of each line, \n will be added by default.

Example:

f = open('about.txt', 'rt')
content = f.readlines()
print(content)
f.close()

Output:

['Hello,\n', 'We are learning how to read and write files']


4. splitlines()

Reads all the lines of the files and returns as a list but there won’t be a ‘\n’ added at the end of each element in a list.

Example:

f = open('about.txt', 'rt')
content = f.read().splitlines()
print(content)
f.close()

Output:

['Hello,', 'We are learning how to read and write files']
5. f.tell()

Returns an integer value giving the current position of the cursor location in the file.

Example:

f = open('about.txt', 'rt')
content = f.read(6)
print(content)
print("Current location of the cursor is at",f.tell())
f.close()

Output:

Hello,
Current location of the cursor is at 6

6. f.seek(offset, whence)

It changes the current position of the cursor location in the file. The position is computed from adding offset to a reference point. The reference point is selected by the whence argument. A whence value of 0 measures from the beginning of the file, 1 uses the current file position, and 2 uses the end of the file as the reference point. The whence can be omitted and defaults to 0, using the beginning of the file as the reference point.

Example:

f = open('about.txt', 'rt')
content = f.read(6)
print(content)
print("current location of the cursor is at ",f.tell())
print("New location of cursor: ", f.seek(2))
print("Printing 3 char after seeking the cursor at 2: ",f.read(3))
f.close()

Output:

Hello,
current location of the cursor is at  6
New location of cursor:  2
Printing 3 char after seeking the cursor at 2:  llo


7. f.write(string)

Writes the content of string to the file, returning the number of characters written.

Example:

with open('write.txt', 'w') as f:
    f.write('Writing to a file.\n')
    f.write('This is second line.')

Output:

A write.txt file will be automatically created in the same directory where your above script is saved and the text written inside the file will be:

Writing to a file.
This is second line.

In the above example, the mode is ‘w’, which means that the file is open for writing. We can do the same with the mode ‘a’ i.e. append: open for writing and appending to the end of the file if it exists, if not it will create a new file and append on it.

Example:

with open('write1.txt', 'a') as f:
    f.write('Writing to a file.\n')
    f.write('This is second line')

Output:

A new file write1.txt will be created and the following text will be appended:

Writing to a file.
This is second line

You can also open a file to read as well as to write in it. For that, we use an ‘r+’ mode. By default, the text will be appended at the beginning, so if you want to append at a customize cursor location, use seek() method.

Example:

with open('write.txt', 'r+') as f:
    f.write('Added new line here')
    f. seek(22)
    f.write('100')

Output (write.txt):

Added new line here
Th100is second line

You may explore more on python file handling in official python documentation.