Understanding File Handling in Python, with Examples

    Ini Arthur
    Share

    In this article on handling files with Python, you’ll learn how to use the Python OS module and how to navigate through local files and directories. You’ll also learn how to open, read, write and close files in Python.

    File handling is a good way to persist data after a program terminates. Data from a computer program is saved to a file and can be accessed later. Python, like many other programming languages, provides handy methods to create, open, read and write data to a file.

    Contents:

    1. File Handling in Python: Files and File Paths
    2. File Handling in Python: Reading and Writing Data

    File Handling in Python: Files and File Paths

    Files are quick alternatives for persisting data from a computer program. The random-access memory (RAM) can only store data temporarily, as all the previous data is lost immediately after the computer system is turned off. Files are preferred, because they’re a more permanent storage point for data on a computer. A file is a location on a local disk where data is stored. It has two essential properties: a filename, and its path.

    Using the OS module

    Python provides an inbuilt OS module for interacting with our computer’s operating system. The OS module has interfaces (functions) that help with performing operations like navigating through directories and files in Python, creating folders, identifying file paths, and so on.

    To use the OS module, we import it into our program as shown below:

    import os
    

    How to get the current working directory

    We can get the current working directory (“cwd”) in Python using the getcwd() method. This method returns the path of the directory we’re currently working in as a string, as shown in the code snippet below:

    import os 
    
    directory = os.getcwd()
    print(directory)
    >>>>
    /home/ini/Dev/Tutorial/sitepoint
    

    Absolute vs relative paths

    File paths can be specified in two ways: either by their absolute path, or by their relative path. Both paths point to the current file location.

    The absolute path of a file declares its path, beginning with the root folder. An absolute path looks like this:

    /home/ini/Dev/Tutorial/sitepoint/filehandling.py
    

    The root folder (as seen in the above code) is home on a Linux OS.

    The relative path of a file declares its path in relation to the current working directory. Let’s see an example:

    ./sitepoint/filehandling.py
    

    The code above shows the relative path for the Python file filehandling.py.

    How to create a directory in Python

    The OS module has a mkdir() method for creating new folders or directories in Python. The mkdir() method takes one argument — a name for the directory — to be created in the current directory as a default behavior. See the code below:

    import os
    
    os.mkdir("photos")
    

    However, we can create directories in a different location by specifying the file path. In the code example below, a projects folder is created in the Tutorial folder:

    os.mkdir("/home/ini/Dev/Tutorial/projects")
    

    When we check inside the Tutorial folder, we’ll find the newly created projects folder.

    How to change the current working directory

    To switch between directories, use the chdir() method. The new path is passed in as an argument to the method to change from the current working directory to another one.

    After creating a new folder in the previous code sample, we can change the directory to the projects folder:

    import os
    
    os.chdir("/home/ini/Dev/Tutorial/projects")
    

    To confirm the change in the directory, use the getcwd() method, which returns a string of the current working directory: /home/ini/Dev./Tutorial/projects.

    How to delete files or directories in Python

    Files and directories can be deleted in Python using the OS module’s remove() and rmdir() methods respectively.

    To delete files in Python, input the file path in the os.remove() method. When deleting files, if the file doesn’t exist, the program will throw the FileNotFoundError.

    Let’s take a code example:

    import os
    
    os.remove("random.txt")
    

    To delete or remove a directory, use os.rmdir(), passing in the directory path to be deleted, like so:

    import os
    
    os.rmdir("/home/ini/Dev/Tutorial/projects")
    

    The projects folder is deleted from the Tutorial folder.

    How to list files and directories in Python

    To get an overview of all the content of a directory, use the os.listdir() method. This method returns a list of all the existing files and directories in that particular folder:

    import os
    
    print(os.listdir())
    >>>>
    ['array.py', 'unittesting.py', 'search_replace.py', '__pycache__', 'pangram.txt', '.pytest_cache', 'exception.py', 'files.py', 'regex.py', 'filehandling.py']
    

    File Handling in Python: Reading and Writing Data

    File handling in Python is simple and not as complicated as it can be in other programming languages. There are different file access modes to choose from when opening a Python file for any operation:

    • r: opens a file for reading. The read mode throws an error when the file doesn’t exist.

    • r+: opens the file to read and write data into a file object. An error is thrown if the file doesn’t exist.

    • w: a file is opened in this mode for writing data. The write mode overrides existing data and creates a new file object if it doesn’t exist.

    • w+: opens a file to read and write data. Existing data on file is overridden when opened in this mode.

    • a: the append mode appends to a file if the file exists. It also creates a new file if there’s no existing file. It doesn’t override existing data.

    • a+: this mode opens a file for appending and reading data.

    • x: the create mode is used to create files in Python. An error is thrown if the file exists.

    Adding b to any of the access modes changes it from the default text format to a binary format (for example, rb, rb+, wb, and so on).

    How to open a file in Python

    To open a file in Python, the open() function is used. It takes at least two arguments — the filename, and the mode description — and returns a file object. By default, a file is opened for reading in text mode, but we can specify if we want the binary mode instead.

    A simple syntax to open a file looks like this:

    f = open('filename', 'mode')
    

    After this step, as seen in the code above, we can begin our read–write operations on the file object. In default mode, files are always handled in text mode.

    How to close a file in Python

    After a file object is opened and file processing operations are carried out, we close the file. It’s often the last step in reading or writing files in Python. The file object close() method is used to close earlier opened files.

    Closing files in Python looks like this:

    f = open('filename', 'mode')
    // file operations, reading, writing or appending
    f.close()
    

    The with statement

    It’s a standard practice to close files after they’ve been opened and file operations have been carried out. It’s possible to miss out on closing some files after they’ve been opened.

    The with statement automatically closes files after the last file handling operation is completed in its scope. For example:

    with open('random.txt', 'r') as f:
        print(f.read())
    >>>>
    Hello world!
    Hello world!
    

    As seen in the code snippet above, the with statement implicitly closes the file after the print statement.

    How to read a file in Python

    There are a couple of ways to read data from a file in Python. We can read a file’s contents using the read(), readline(), and readlines() methods.

    The read() method

    The read() method returns a string of all characters on the file being read. The pointer is placed at the start of the file content. The default mode is to read from the beginning of the file to the end of the file, except where the number of characters is specified.

    Take a look at the code snippet below:

    f = open('random.txt', 'r')
    print(f.read())
    f.close()
    >>>>
    This is some random text.
    Here is the second line.
    The sky is blue.
    Roses are red. 
    

    We can specify how many characters to read from the text file. Simply pass the number of characters as an argument to the read() method:

    f = open('random.txt', 'r')
    print(f.read(12))
    f.close()
    >>>>
    This is some
    

    As seen in the example above, the intepreter reads only twelve characters from the entire file.

    The readline() method

    This method reads one line from a file at a time. It reads from the beginning of the file and stops where a newline character is found. See the code example below:

    f = open('random.txt', 'r')
    print(f.readline())
    f.close()
    >>>>
    This is some random text.
    

    The readlines() method

    This method returns a list of all lines from the existing file being read. See the code snippet below:

    f = open('random.txt', 'r')
    print(f.readlines())
    f.close()
    >>>>
    ['This is some random text.\n', 'Here is the second line.\n', 'The sky is blue.\n', 'Roses are red.']
    

    Note: all the methods for reading a file stream return an empty value when the end of the file is reached. The seek() method returns the file cursor to the beginning of the file.

    How to write to a file in Python

    The write() method in Python is useful when attempting to write data to a file. To write to an opened file, the access mode should be set to one of the following: w, w+, a, a+, and so on. Once again, the default is text mode rather than binary.

    The write() method

    Pass a string argument to this method when we want to write data to a text file. Remember that the write mode will override existing data if the file exists:

    f = open('random.txt', 'w')
    f.write("Hello world!")
    f.close()
    

    The writelines() method

    This method helps us insert several strings to a text file at once. We can write multiple lines of strings to a file in one go by passing the list as an argument of the method:

    words = ['The sky is blue', '\nRoses are red']
    
    f = open('random.txt', 'w')
    f.writelines(words)
    f.close()
    

    The code above shows a closed file being opened and some lines in a word list being inserted at once into the random.txt text file.

    Conclusion

    There are two important attributes about a file: the filename and its path. The OS module helps us navigate through directories and perform certain operations. Python file handling involves using several methods to open, create, read, and write data on file objects.

    It’s also important to understand file handling in Python if we want to interact with content inside text or binary files. Always make sure to close files after carrying out operations on them. The with statement makes it easier to perform file handling in Python, as it implicitly closes file objects after we’re done.

    Related reading: