| by Arround The Web | No comments

Python File Write() Method

Python supports various inbuilt methods for handling files and directories. These methods are used to read, write or append data to particular files, such as Txt, CSV, or binary. To write data directly to the file, the inbuilt “file.write()” method is utilized in Python. This method is used along with the “open()” function that is used to open the file in the specified mode.

This write-up will deliver you with a comprehensive guide on the “file.write()” method using multiples examples and via the following content:

What is the “file.write()” Method in Python?

In Python, the “file.write()” method is employed to write the text to the specified files based on the file mode. The file mode “a” is used when we insert the new text file at the end of the original file, while the “w” mode is used to write the text file by clearing the original text file.

Syntax

file.write(byte)

 

Parameters

In the above syntax, the “byte” parameter specifies the “text” or “byte” object that needs to be inserted into the file.

Return Value

The “file.write()” method does not retrieve any value.

Example 1: Writing String to Text File Using the “write()” Method

In the below example code, the “open()” function opens the file named “new.txt” in write “w” mode and writes the specified string to the file utilizing the “write()” method. The file is closed after writing the string using the “file.close()” method:

file1 = open('new.txt', 'w')
file1.write("Welcome to Python Guide\nLinuxhint")
file1.close()

 

The execution of the above code created a file with specified string data:

Example 2: Appending String to Text File Using the “write()” Method

In the following code, the “a” append mode parameter, along with the file name, is passed to the “open()” function to open the file in appending mode. After opening the file, the “write()” method writes the string to the file end data and terminates the file using the “file.close()” method.

file1 = open('new.txt', 'a')
file1.write("\n\nWelcome to Linux Guide\nLinuxhint")
file1.close()

 

The above code appends the new text to the end of the file, as shown in the below snippet:

Example 3: Reading the File in Python Using the “file.read()” Method

After writing the text file, sometimes users want to read the file data in Python. For this purpose, the specified file is opened in read “r” mode, and the data is read using the “read()” method. Here is an example that read the file:

file1 = open('new.txt', 'r')
print(file1.read())
file1.close()

 

The execution of the above code shows the below output snippet:

Example 4: Writing String to Text File Using the “with” Statement and “write()” Method

The “with” statement can be used before the “open()” function to open the file in “w” mode and assign the file data to the “file1” object. We do not need to close/terminate the file when we open the file by utilizing the “with” statement. The “file1.write()” method is used to write the string to the text file:

with open("new.txt", "w") as file1:
    file1.write("Hello and Welcome to Linuxhint")

 

This code generated the below output:

Example 5: Writing Unicode Character String to Text File Using the “write()” Method

The “encoding=utf-8” parameter is passed to the “open()” function along with the “w” write mode parameter to encode the string data and insert it into the file using the “write()” method. Let’s perform this via the following example code:

str1 = '!Øڃಚếệ'
with open('new.txt', 'w', encoding='utf-8') as f:
    f.write(str1)

 

The above code execution retrieves the below output:

Conclusion

The “file.write()” method in Python is used to write or insert the text to the specified files based on the file mode. This method can be used to append the file to the end or write the file by removing all the content from the original file. This write-up presented a detailed tutorial on Python’s “file.write()” method using numerous examples.

Share Button

Source: linuxhint.com

Leave a Reply