How to write a single line in text file using Python?


Python has built in file creation, writing, and reading capabilities. In Python, there are two sorts of files that can be handled: text files and binary files (written in binary language, 0s, and 1s).

In this article, we'll look at how to write into a file.

Firstly, we will have to open the file in write mode using the open() function. Then, the write() method saves a file with the provided text. The file mode and stream location determine where the provided text will be put.

  • "a" − The text will be put at the current location in the file stream, which is usually at the end of the file.

  • "w" − The file will be emptied before the text is inserted at the current file stream position, which is set to zero by default.

Syntax

The following is the syntax of the open() method.

file = open("file_name", "access_mode")

Example 1

Let us look at an example of opening a file in write mode. If the example.txt file doesn’t exist, the open() function will create a new file.

file = open('example.txt', 'w')

Output

On executing the above program, the following output is generated.

The file example.txt is opened in write mode.

Example 2

In the following example, a file is opened in the write mode using the open() method. Then, with the help of the write() method, the text is written in the file and then closed using the close() method.

#python program to demonstrate file write()
file = open('example.txt', 'w')
file.write( "the end")
file.close()

Output

On executing the above program, the following output is generated.

The text "the end" is written into the file. The previous contents of the file have been cleared.

From the previous example, we have written into a file using the write mode. To write into a file without clearing the previous content we can write into the file using the append ‘a’ mode. To write into a file using append mode, open the file in append mode, with either 'a' or 'a+' as the access mode, to append a new line to an existing file. The following are the definitions of these access modes: Only append ('a'): To begin writing, open the file. If the file does not exist, it is created. The file's handle is located at the very end.

Example 3

In the following example, a file named example.txt is opened in the append mode using the open() function. Then, the text is written into the file using the write() function.

#python program to demonstrate file write in append mode
file = open('example.txt', 'a')
file.write( "the end ")

Output

On executing the above program, the following output is generated.

The text is appended to the file.

Example 4

In the following example, a file is opened in append mode using the open() function. Then, the text is written into the file using the write() function and the file is closed using the close() function.

#python program to demonstrate file write in append mode
f = open('myfile', 'a')
f.write('hi there\n') 
# python will convert \n to os.linesep
f.close()

Output

On executing the above program, the following output is generated.

The text is appended in the file in a next line.

Updated on: 11-May-2023

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements