Nearby lessons

89 of 159

Python - Write Files

📌 What You Will Learn
  • Write a single string to a text file using write()
  • Write multiple lines from a list using writelines()
  • Understand why the newline character \n is required
  • Explain the difference between write() and writelines()
  • Know when to open a file in append mode instead of write mode

Writing Data to Text Files

We can write character data to text files by using the following two methods:

  • write(str)
  • writelines(list of lines)

Overview of Writing Methods

Method Purpose
write(str) Writes a single string to a text file.
writelines(list) Writes multiple strings from a list into a text file.

The write() Method

The write() method is used to write character data to a text file.

The parameter str represents the string to be written into the file.

🐍Code Cell
1f.write(str)
Output
No output captured.

Example 1: Writing Data by Using write()

🐍Code Cell
1f = open("abcd.txt", "w")
2 
3f.write("Durga\n")
4f.write("Software\n")
5f.write("Solutions\n")
6 
7print("Data written to the file successfully")
8 
9f.close()
Output
Data written to the file successfully

Contents of abcd.txt

Explanation

  • open("abcd.txt", "w") opens the file in write mode. If the file already exists, its previous contents are overwritten. If it does not exist, Python creates a new file.
  • Each f.write("...\n") call writes one line. The \n moves the cursor to the next line.
  • f.close() closes the file after completing the write operation.

Important Note

If we execute the above program again, the existing data in the file will be overwritten every time.

Instead of overwriting, if we want to append data, then we should open the file as follows:

🐍Code Cell
1f = open("abcd.txt", "a")
Output
No output captured.

Append Mode

Opening a file in a (append) mode preserves the existing contents and adds the new data at the end of the file.

Importance of the Newline Character (\n)

When writing data by using the write() method, we must provide the line separator \n.

Otherwise, all the data will be written into a single line.

Example: Writing Without Using \n

🐍Code Cell
1f = open("abcd.txt", "w")
2 
3f.write("Durga")
4f.write("Software")
5f.write("Solutions")
6 
7f.close()
Output
No output captured.

Contents of abcd.txt

Explanation

Since the newline character (\n) is not used, the cursor remains on the same line after every write.

As a result, all three strings are written continuously in a single line: DurgaSoftwareSolutions.

Correct Way: Using the Newline Character

🐍Code Cell
1f = open("abcd.txt", "w")
2 
3f.write("Durga\n")
4f.write("Software\n")
5f.write("Solutions\n")
6 
7f.close()
Output
No output captured.

Contents of abcd.txt

Explanation

Each call to write() includes the newline character (\n), so the cursor moves to the next line after every string.

Each string is therefore written on a separate line.

How the Newline Character Works

The newline character (\n) tells Python to move the cursor to the next line after writing the current string.

Without \n, the next string is written immediately after the previous string without any line break.

Without \n
-----------
write("Durga")
write("Software")
write("Solutions")

Output:
DurgaSoftwareSolutions

With \n
--------
write("Durga\n")
write("Software\n")
write("Solutions\n")

Output:
Durga
Software
Solutions

The writelines() Method

The writelines() method is used to write multiple lines from a list into a text file.

🐍Code Cell
1f.writelines(list_of_lines)
Output
No output captured.

Example 2: Writing Multiple Lines

🐍Code Cell
1f = open("abcd.txt", "w")
2 
3list = ["sunny\n", "bunny\n", "vinny\n", "chinny"]
4 
5f.writelines(list)
6 
7print("List of lines written to the file successfully")
8 
9f.close()
Output
List of lines written to the file successfully

Contents of abcd.txt

Explanation

The list contains four strings. Notice that the first three strings contain the newline character (\n), while the last string does not.

f.writelines(list) writes all strings from the list into the file in the same order.

Difference Between write() and writelines()

Method Description
write(str) Writes a single string to the file.
writelines(list) Writes multiple strings from a list.
📝 Key Takeaways
  • write(str) writes a single string to the file
  • writelines(list) writes multiple strings from a list in sequence
  • write() does not add a newline automatically - you must include \n explicitly
  • Without \n, all strings are written continuously on a single line
  • Opening a file in w mode overwrites existing data; a mode appends to it

🧠 Test Your Knowledge

8 Questions
Progress: 0 / 8