Nearby lessons
89 of 159Python - Write Files
- 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.
Example 1: Writing Data by Using write()
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\nmoves 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:
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
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
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.
Example 2: Writing Multiple Lines
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. |
- 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