HomeLinuxPython Write String to File

Python Write String to File


The time period “file” refers to a group of information positioned at a specific location within the system. Python gives numerous modules and features to learn and write recordsdata. “Writing to a file” is a standard process in Python that’s assistive largely on the developer’s finish. This entails file opening, writing information to it, after which closing the file.

The aim of this weblog is to clarify the way to write a string to a specified file utilizing numerous approaches.

The way to Write Python String to File?

To jot down a string to the actual file in Python, think about the beneath approaches:

Technique 1: Writing String to a File Utilizing the “write()” Technique

Essentially the most simple method to write a string to a file is by way of the “write()” technique. This technique writes a string to the file opened in “write” mode. Let’s perceive it by the beneath instance:

Instance

The beneath code is used to jot down a string to a file by way of the mentioned technique:

file = open(“new_file.txt”, “w”)
file.write(“Hiya, world!”)
file.shut()

 
Within the above code snippet:

    • The file named “new_file.txt” is opened in “write” mode utilizing the “open()” operate.
    • The desired string is written to the actual file by way of the “write()” technique.
    • The final step is to shut the file by using the “shut()” technique.

Output


Within the above output, it may be clearly seen that the desired string worth has been written to a file.

Technique 2: Writing String to a File Using “with” Assertion

The “with” assertion gives a extra environment friendly means of writing to a file. It robotically closes the file after the block of code has been executed which will be demonstrated within the following instance.

Instance

Let’s overview the next instance:

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

 
Within the above traces of code:

    • Likewise, the file named “new_file.txt” is opened in “write” or “w” mode.
    • After that, equally, the string “Python world!” is written to the desired file using the “write()” technique.
    • Lastly, the “with” assertion robotically closes the file when the code block is accomplished.

Output


The above output exhibits that the desired string worth has been written to a file.

Technique 3: Writing String to a File Utilizing the “fileinput” Module

The “fileinput” module writes a string to a file by changing the complete file content material.

Instance

The next code writes the desired string worth to a file utilizing the “fileinput” module:

import fileinput
with fileinput.FileInput(“new_file.txt”, inplace=True) as file:
    for line in file:
        print(“Python Information”, finish=“”)

 
Within the above code block:

    • The “fileinput” module is used to open the file named “new_file.txt” in “inplace” mode, which implies that the output is written to the identical file.
    • Then, the “for” loop is used to iterate by way of the traces of the file and exchange the content material of every line with the string “Python Information” by way of the “print()” operate.

Output


Based on the above output, the string has been written to the file efficiently.

Conclusion

To jot down a string to a file in Python, apply the “write()” technique, “with” assertion, or the “fileinput” module. The “write()” technique is used together with the “open()” and “shut()” features to jot down a specified string to the file. Equally, the “with” assertion is used together with the “open()” operate to jot down a string to a file. The “fileinput” module nonetheless replaces the file content material with the written string. This Python publish introduced numerous methods to create a textual content file from a string.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments