- How to Write String to Text File in Python?
- Examples
- 1. Write string to text file
- 2. Write string to text file in text mode
- Writing other than String to Text File
- Summary
- Python Write String to File
- How to Write Python String to File?
- Method 1: Writing String to a File Using the “write()” Method
- Method 2: Writing String to a File Utilizing “with” Statement
- Method 3: Writing String to a File Using the “fileinput” Module
- Conclusion
- About the author
- Talha Saif Malik
- Python Write String to File
- How to Write Python String to File?
- Method 1: Writing String to a File Using the “write()” Method
- Method 2: Writing String to a File Utilizing “with” Statement
- Method 3: Writing String to a File Using the “fileinput” Module
- Conclusion
- About the author
- Talha Saif Malik
How to Write String to Text File in Python?
Now you can save or write string to text a file in persistent data storage using Python.
To write string to a Text File, follow these sequence of steps:
- Open file in write mode using open() function.
- Write string to the file using write() method.
- Close the file using close() method.
Examples
1. Write string to text file
In the following example, we will take a string constant and write the string to a text file by following the above said sequence of steps.
Python Program
text_file = open("sample.txt", "w") n = text_file.write('Welcome to pythonexamples.org') text_file.close()
The write() method returns the number of characters written to the text file.
Note that this kind of writing to text file, overwrites the data, if the file is already present. If the file is not present, it creates a new file and then writes the string to the file.
2. Write string to text file in text mode
A file can be opened in two modes: the first one is text mode and the second one is binary mode. By default a file is opened in text mode. However, you can explicitly specify the mode.
In the following example, we will open the file in text mode by appending “t” to the mode and write the string to a text file by following the sequence of steps mentioned at the start of this tutorial.
Python Program
text_file = open("sample.txt", "wt") n = text_file.write('Welcome to pythonexamples.org') text_file.close()
Writing other than String to Text File
If you would like to write any Python object other than string or a bytes object to a file, using write() method, you should first convert that Python object to a string or bytes object.
Summary
In this tutorial of Python Examples, we learned how to write a string to a text file, with the help of example programs.
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:
The below code is used to write a string to a file via the discussed method:
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.
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.
Let’s overview the following example:
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.
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.
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 = «» )-
- 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.
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.
About the author
Talha Saif Malik
Talha is a contributor at Linux Hint with a vision to bring value and do useful things for the world. He loves to read, write and speak about Linux, Data, Computers and Technology.
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:
The below code is used to write a string to a file via the discussed method:
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.
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.
Let’s overview the following example:
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.
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.
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 = «» )-
- 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.
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.
About the author
Talha Saif Malik
Talha is a contributor at Linux Hint with a vision to bring value and do useful things for the world. He loves to read, write and speak about Linux, Data, Computers and Technology.