Working with files in Python

Coding with Python

🕑 This lesson will take about 20 minutes

Python includes different functions and methods used to manipulate files (such as text files). In Python, you can read data from a file, write data to a file, and append data to an existing file. When opening a file in Python, you can specify which mode you are opening the file in. These modes include:

  • r (read)

  • w (write)

  • a (append)

Important: When using a code editor such as Visual Studio Code, make sure you open the folder (that contains your Python code and the file you wish to read/write/append) before running the code. If the folder is not open in Visual Studio Code, then it won’t be able to access any files in it. If running the code in Terminal or Command Prompt, then just make sure you use the cd command to specify the directory (folder) where the code and file you wish to open are stored.

Let’s check out some examples of reading, writing and appending data to files using Python code.

Reading the entire contents of a file

Here is an example of opening and reading the entire contents of an existing text file. When using the with open statement, you need to specify the file name including its extension (eg. my_textfile.txt) and specify the mode to open the file in (eg. 'r' means read mode). This code will open the file and then use the file.read() function to read all of the contents which will then be displayed on screen (if there are multiple lines of text in the file, they’ll all be displayed). The file will be closed once its contents have been read and displayed.

Writing to a file

Write (w) mode is used to write contents to a file. In the example below, a text file is opened, then 'w' (write) is specified as the mode to open the file in. The file.write() function is then used to write lines of content to the text file. In thix example the \n characters are used to create a new line in the text file. Note that when in write mode, Python will overwrite the existing content in the file with the new content. In addition, if the file doesn’t already exist, then it will be created and then written to.

Appending to a file

Appending means to add to the existing content in a file. If we append new data to a text file, the new data will be added to the end of the existing data in the file. In the example below, Python opens a text file in append (a) mode. Then the file.write() function is used to append new data to the end of any existing data that might be in the text file. The \n characters are also used in this example so that a new line is created before any text is appended.

Reading from a file, line by line

In this example, a for loop is used to read a text file line-by-line. Each line of text in the file can be processed in each iteration of the loop, one by one.

Read lines from a file into a list

In this example, a text file is opened and each line of text from the file is read into a list. This means that each line of text from the file will now be stored as elements in the list. We can then use that list how we like throughout the program without needing the file anymore. This could be useful, for example, if each line of text in a file was an individual data item (such as a username, or a word, a number, or product details, etc.).

Copy elements from a list to a file

In this last example, we have a list of values (a list of names in this example). We also have an empty text file called 'my_text_file.txt'. The with open statement is used to open the text file in append (a) mode and then a for loop is used to iterate over each element (name) in the list. Each element is appended, one by one, to the text file (each value will be on a new line). Note that the \n character is used to create a new line after each element from the list is added to the text file. You can modify this code to suit your situation, depending on whether the file already exists or doesn’t exist, and whether it will already contain data, or you want to overwrite existing data first.