
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Write Multiple Lines in Text File Using Python
Python has built-in functions for creating, reading, and writing files, among other file operations. Normal text files and binary files are the two basic file types that Python can handle. We'll look at how to write content into text files in Python in this article.
Steps involved writing multiple lines in a text file using Python
Following are the ways to write multiple lines in text file using Python ?
- The open() method must be used to open the file for writing, and the function must be given a file path.
- The following step is to write to file. Several built-in methods, such as write() and writelines, can be used to do this ().
- The text file has to be closed using the close() method after the writing process is finished.
Note ? All the examples mentioned below follows the above mentioned steps.
Open() Function
If opening the file is possible, the open() function does it and returns the matching file object.
There are numerous parameters for the open() function. Let's examine the parameters required for writing to a text file. It returns a file object after opening the file in the chosen mode.
Syntax
file = open('filepath','mode')
Where,
- filepath ? It represents the path of the file.
- mode ? It holds numerous optional parameters. It is a string that indicates the opening mode for the file.
Using writelines() Function
This function writes several string lines to a text file simultaneously. An iterable object, such as a list, set, tuple, etc., can be sent to the writelines() method.
Syntax
file.writelines(list)
Where list is the collection of texts or bytes that will be added. It could be a string collection, tuple, list, etc.
Example - 1
Following is an example to write multiple lines in a file using Python ?
with open('file.txt', 'a') as file: l1 = "Welcome to TutorialsPoint\n" l2 = "Write multiple lines \n" l3 = "Done successfully\n" l4 = "Thank You!" file.writelines([l1, l2, l3, l4])
Output
As an output we get a text file named as "file" with the following lines written in it ?
Welcome to TutorialsPoint Write multiple lines Done successfully Thank You!
Example - 2
Following is an alternate example to write multiple lines in a file using Python ?
with open("file.txt", "w") as file: lines = ["Welcome to TutorialsPoint\n", "Write multiple lines \n", "Done successfully\n" ] file.writelines(lines) file.close()
Output
As an output, we get a text file named as "file" with the following lines written in it ?
Welcome to TutorialsPoint Write multiple lines Done successfully
Example - 3: Using while loop
Following is an example to write multiple lines in a file using while loop ?
# declare function count() def write(): # open a text file in read mode and assign a file object with the name 'file' file=open("file.txt",'w') while True: # write in the file l=input("Welcome to TutorialsPoint:") # writing lines in a text file file.write(l) next_line=input("The next line is printed successfully:") if next_line=='N': break file.close() write()
Output
Following is an output of the above code ?
Welcome to TutorialsPoint: The next line is printed successfully:
Using the writelines() Function
If you want to add more lines to an existing text file, you must first open it in append mode and then use the writelines() function, as seen below.
Example
Following is an example to append multiple lines in a text file ?
with open("file.txt", "a") as f: lines = ["Adding lines\n", "writing into it \n", "written successfully\n" ] f.writelines(lines) f.close()
Output
We get multiple lines appended in the already existing file ?
Welcome to TutorialsPoint Write multiple lines Done successfully Adding lines writing into it written successfully