Python File Handling A-Z Guide for Beginners

Python file handling is one of the essential topics for programmers and automation testers. As both of them needs to work with files either to write to a file or to read data from it. Also, if you are not already aware, I/O operations are the costliest operations where a program can stumble. Hence, you should be quite careful while implementing file handling for reporting or any other purpose. Optimizing a single file operation can help you produce an high-performing application or a robust solution for automated software testing.

Let’s take an example, say, you are going to create a big project in Python which contains a no. of workflows. Then, it’s inevitable for you not to create a log file. And you’ll also be doing both read/write operations on the log file. Log files are a great tool to debug large programs. It’s always better to think about a scalable design from the beginning, as you won’t regret it later that you didn’t do it. So, don’t mind reading the ten Python coding tips that can guide you in writing better code.

Here is a basic definition of file handling in Python, “File is a named location on the system storage which records data for later access. It enables persistent storage in a non-volatile memory i.e. Hard disk.

In Python, file processing takes place in the following order.

  • Open a file which returns a file handle.
  • Use the handle to perform read or write action.
  • Close the file handle.

Before you do a read or write operation to a file in Python, you need to open it first. And as the read/write transaction completes, you should close it to free the resources tied with the file.

In the next sections, we’ll touch upon all the Python file handling topics one by one. Since it’s an example-driven Python tutorial, so better you open a Python console to test-run the code. Else you can also launch an online Python terminal, read this Python article on our blog.

Python File Handling A-Z Guide for Beginners.

First of all, let’s look at the summary of the Python file handling topics covered in this tutorial.

  1. Open a file in Python.
    1. Python open() file method.
    2. File open modes in Python.
    3. The Python file object attributes.
    4. Python file encoding.
  2. Close a file in Python.
  3. Python Write operation.
    1. The write() file method.
  4. Python Read operation.
  5. File read positions in Python.
  6. Renaming and deleting files in Python.
    1. The rename() method.
    2. The remove() method.
  7. Python file object methods.

1- Python file handling: Open a file in Python.

To read or write to a file, you need to open it first. To open a file in Python, use its built open() function. This function returns a file object i.e. a handle. You can use it to read or modify the file.

1.1- Python open() file method.

Below are the parameter details.

<access_mode>- It’s an integer representing the file open mode e.g. read, write, append, etc. It’s an optional parameter. By default, it is set to read-only <r>. In this mode, we get data in text form after reading from the file. On the other hand, binary mode returns bytes. It’s preferable for accessing the non-text files like an image or the Exe files. See the table in the next section. It lists down the available access modes.

<buffering>- The default value is 0 which means buffering won’t happen. If the value is 1, then line buffering will take place while accessing the file. If it’s greater than 1, then the buffering action will run as per the buffer size. In the case of a negative value, the default behavior is considered.

<file_name>- It’s a string representing the name of the file you want to access.

1.2- File open modes in Python.

Modes Description
<r> It opens a file in read-only mode while the file offset stays at the root.
<rb> It opens a file in (binary + read-only) modes. And the offset remains at the root level.
<r+> It opens the file in both (read + write) modes while the file offset is again at the root level.
<rb+> It opens the file in (read + write + binary) modes. The file offset is again at the root level.
<w> It allows write-level access to a file. If the file already exists, then it’ll get overwritten. It’ll create a new file if the same doesn’t exist.
<wb> Use it to open a file for writing in binary format. Same behavior as for write-only mode.
<w+> It opens a file in both (read + write) modes. Same behavior as for write-only mode.
<wb+> It opens a file in (read + write + binary) modes. Same behavior as for write-only mode.
<a> It opens the file in append mode. The offset goes to the end of the file. If the file doesn’t exist, then it gets created.
<ab> It opens a file in (append + binary) modes. Same behavior as for append mode.
<a+> It opens a file in (append + read) modes. Same behavior as for append mode.
<ab+> It opens a file in (append + read + binary) modes. Same behavior as for append mode.

 

1.3- The Python file object attributes.

When you call the Python open() function, it returns an object which is the file handle. Also, you should know that Python files have several linked attributes. And we can make use of the file handle to list the attributes of a file it belongs.

For more information on file attributes, please run down through the below table.

Attribute Description
<file.closed> For a closed file, it returns true whereas false otherwise.
<file.mode> It returns the access mode used to open a file.
<file.name> It returns the name of a file.
<file.softspace> It returns a boolean to suggest if a space char will get added before printing another value in the output of a <print> command.

 

Example: Python file attribute in action.

 

1.4- Python file encoding.

In Python 3.x, there is a clear difference between strings (text) and a byte (8-bits). It states that the char ‘a’ doesn’t represent the ASCII value 97 until you specify it like that. So, while you want to use a file in text mode, then better you mention the correct encoding type.

Also, Python stores a file in the form of bytes on the disk, so you need to decode them in strings before reading. And, similarly, encode them while writing texts to the file.

For a note, Python enables platform-dependent encoding by default. Hence, if you don’t change it, then it’s set to <cp1252> for Windows and <utf-8> for Linux.

Thus, the documentation says to quote the desired encoding while opening a file in Python. See the below Python code snippet.

For a note, you should import the <io> module in Python 2.x to enable the encoding feature. Python 3.x does it implicitly.

 

2- Python file handling: Close a file in Python.

It’s always the best practice to close a file when your work gets finished. However, Python runs a garbage collector to clean up the unused objects. But you must do it on your own rather leave it for the GC.

The close() file method.

Python provides the <close()> method to close a file.

While closing a file, the system frees up all resources allocated to it. And it’s rather easy to achieve.

Please see from the below code snippets.

Example-1: Basic close operation in Python.

The most basic way is to call the Python close() method.

 

Example-2: Using exception with close operation in Python.

Say, if an exception occurs while performing some operations on the file. In such a case, the code exits without closing the file. So it’s better to put the code inside a <try-finally> block.

So, even if there comes an exception, the above code will make sure your file gets closed properly.

 

Example-3: Close a file using ‘with’ clause in Python.

Another way to close a file is by using the <with> clause. It ensures that the file gets closed when the block inside the <with> clause executes. The beauty of this method is that it doesn’t require to call the close() method explicitly.

 

3- Python file handling: Perform Write operation.

While you get ready for writing data to a file, first of all, open it using a mode which has as a prefix. To view the list of all available file modes, click here.

You can even do the same using the append mode. Also, if you’ve used the <w> mode, then it’ll erase the existing data from the file. So you must note this fact while you choose it.

The write() file method.

Python provides the <write()> method to write a string or sequence of bytes to a file. This function returns a number which is the size of data written in a single Write call.

Example: Read/Write to a File in Python.

 

4- Python file handling: Perform Read operation.

To read data from a file, first of all, you need to open it in read mode. Then, you can call anyone of the methods that Python provides for reading from a file.

Usually, you can use Python <read(size)> function to read the content of a file up to the size. If you don’t pass the size, then it’ll read the whole file.

Example: Read from a File in Python.

 

5- Python file handling: File read positions.

The <tell()> method gives you the current offset of the file pointer in a file.

The <seek(offset[, from])> method can help you change the position of a file pointer in a file.

The <offset> argument represents the size of the displacement while the <from> argument indicates the start point.

If the <from> value is 0, then the shift will start from the root level. If it’s value is 1, then the reference position will become the current position. Also, if it is 2, then the end of file would serve as the reference position.

Example: Setting offsets in Python.

 

6- Python file handling: Renaming and deleting files in Python.

While you were using the <read/write> functions, you may also need to <rename/delete> a file in Python. So, there comes a <os> module in Python which brings the support of file <rename/delete> operations.

So, to continue, first of all, you should import the <os> module in your Python script.

The rename() file method.

The <rename()> method takes two arguments, the current filename, and the new filename.

Following is the example to rename an existing file <app.log> to <app1.log>.

Example:

 

The remove() file method.

The <remove()> method deletes a file which it receives in the argument.

Following is the example to delete an existing file, the <app1.log>.

Example:

 

7- Python file handling: File object methods.

So far, we’ve only shared with you a few of the functions that you can use for file handling in Python. But there is more to the story of Python file handling.

Since the Python’s open() method returns an object which we call as the file handle. So, Python adds a no. of functions that you can call using this object.

Function Description
<file.close()> Close the file. You need to reopen it for further access.
<file.flush()> Flush the internal buffer. It’s same as the <stdio>’s <fflush()> function.
<file.fileno()> Returns an integer file descriptor.
<file.isatty()> It returns true if file has a <tty> attached to it.
<file.next()> Returns the next line from the last offset.
<file.read(size)> Reads the given no. of bytes. It may read less if EOF is hit.
<file.readline(size)> It’ll read an entire line (trailing with a new line char) from the file.
<file.readlines(size_hint)> It calls the <readline()> to read until EOF. It returns a list of lines read from the file. If you pass <size_hint>, then it reads lines equalling the <size_hint> bytes.
<file.seek(offset[, from])> Sets the file’s current position.
<file.tell()> Returns the file’s current position.
<file.truncate(size)> Truncates the file’s size. If the optional size argument is present, the file is truncated to (at most) that size.
<file.write(string)> It writes a string to the file. And it doesn’t return any value.
<file.writelines(sequence)> Writes a sequence of strings to the file. The sequence is possibly an iterable object producing strings, typically a list of strings.

 

Final Word.

We wish the above Python file handling tutorial would have helped you in learning Python. If you want us to bring more such tutorials, then like and share this tutorial further.

Like we always say that it’s just the beginning, there is a long road ahead.

So keep climbing on the learning ladder.

All the Best,

TechBeamers.

Leave a Reply


*