Introduction:

In Python, writing to files allows us to store data permanently, beyond the runtime of the program. This is a crucial feature when creating logs, saving user data, or exporting information for other systems. Python provides multiple ways to write to files, and we’ll focus on two key methods:

  • write(): Writes a single string to a file.
  • writelines(): Writes a list of strings to a file.

Additionally, files can be opened in different modes:

  • Write Mode (w): This mode overwrites the file if it exists or creates a new one if it doesn’t.
  • Append Mode (a): This mode adds data to the end of the file without overwriting existing content.

Real-World Example:

Imagine you are building a program that keeps track of tasks. You can save these tasks to a file so that they are available the next time the program runs. Using write() or writelines(), you can store each task in a file and use append mode to add new tasks without losing the previous ones.

Let’s look at some examples of writing to and appending data to files.

Example 1: Writing Data to a File Using write()

In the following example, we open a file in write mode and use the write() function to write a string to the file. Notice that if the file already exists, its content will be overwritten.

Here, two lines of tasks are written to tasks.txt. After running the code, open the tasks.txt file to see the newly written content.

Example 2: Writing Multiple Lines Using writelines()

The writelines() method is used to write multiple lines to a file at once. This method expects a list of strings, where each string represents a line.

In this example, we write a list of tasks to the file using writelines(). Notice that each string in the list already includes a newline character (\n).

Example 3: Appending Data to an Existing File Using append Mode (a)

When you need to add new data to an existing file without overwriting its contents, you can open the file in append mode (a). This ensures that any new data is added to the end of the file.

In this example, the new tasks are appended to the existing tasks in tasks.txt. The original content remains intact, and the new tasks are added to the end.

Predict:

Look at the following code snippet. Can you predict what the content of the file will be after running this code?

with open('notes.txt', 'w') as file:
    file.write('Python is fun!\n')

with open('notes.txt', 'a') as file:
    file.write('Learning about file handling.\n') 

What will notes.txt contain after these operations? Think about the difference between write mode and append mode.

Run:

Let’s try running an example that writes and appends to a file. The following code first writes a line to the file and then appends two additional lines.

When you run the code, observe how the file content evolves as you write and append data.

Investigate:

Now let’s investigate the difference between write mode (w) and append mode (a). What happens if we open the same file in write mode again after adding data? Modify the code below to write new content to the file and observe the result.

Run this code and observe how the previous content in notes.txt is overwritten. This demonstrates the importance of using the correct mode for your use case.

Modify:

Let’s modify this code to write multiple lines to a file. Below is a program that writes only one line. Can you change it so that it writes three separate lines to the file?

Hint: You can use multiple write() calls or create a list of strings and use writelines().

Scroll to Top