| by Arround The Web | No comments

Python Write String to File

The term “file” refers to a collection of data located at a particular location in the system. Python provides various modules and functions to read and write files. “Writing to a file” is a common task in Python that is assistive mostly at the developer’s end. This involves file opening, writing data to it, and then closing the file.

The purpose of this blog is to explain how to write a string to a specified file using various approaches.

How to Write Python String to File?

To write a string to the particular file in Python, consider the below approaches:

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

The most straightforward way to write a string to a file is via the “write()” method. This method writes a string to the file opened in “write” mode. Let’s understand it by the below example:

Example

The below code is used to write a string to a file via the discussed method:

file = open("new_file.txt", "w")
file.write("Hello, world!")
file.close()

 
In the above code snippet:

    • The file named “new_file.txt” is opened in “write” mode using the “open()” function.
    • The specified string is written to the particular file via the “write()” method.
    • The last step is to close the file by utilizing the “close()” method.

Output


In the above output, it can be clearly seen that the specified string value has been written to a file.

Method 2: Writing String to a File Utilizing “with” Statement

The “with” statement provides a more efficient way of writing to a file. It automatically closes the file after the block of code has been executed which can be demonstrated in the following example.

Example

Let’s overview the following example:

with open("new_file.txt", "w") as file:
    file.write("Python world!")

 
In the above lines of code:

    • Likewise, the file named “new_file.txt” is opened in “write” or “w” mode.
    • After that, similarly, the string “Python world!” is written to the specified file utilizing the “write()” method.
    • Lastly, the “with” statement automatically closes the file when the code block is completed.

Output


The above output shows that the specified string value has been written to a file.

Method 3: Writing String to a File Using the “fileinput” Module

The “fileinput” module writes a string to a file by replacing the entire file content.

Example

The following code writes the specified string value to a file using the “fileinput” module:

import fileinput
with fileinput.FileInput("new_file.txt", inplace=True) as file:
    for line in file:
        print("Python Guide", end="")

 
In the above code block:

    • The “fileinput” module is used to open the file named “new_file.txt” in “inplace” mode, which means that the output is written to the same file.
    • Then, the “for” loop is used to iterate through the lines of the file and replace the content of each line with the string “Python Guide” via the “print()” function.

Output


According to the above output, the string has been written to the file successfully.

Conclusion

To write a string to a file in Python, apply the “write()” method, “with” statement, or the “fileinput” module. The “write()” method is used along with the “open()” and “close()” functions to write a specified string to the file. Similarly, the “with” statement is used along with the “open()” function to write a string to a file. The “fileinput” module however replaces the file content with the written string. This Python post presented various ways to create a text file from a string.

Share Button

Source: linuxhint.com

Leave a Reply