Python Write String to File

As we all know, Python provides different built-in functions for creating, reading, and writing text files. Python handles two types of files. The first one is normal text files and the second one is binary files (which are written in a binary language or the form of 0s and 1s). In the text file, lines are closed with a special character (\n) known as EOL. It stands for End of Line. On the other hand, a binary file does not contain any line terminator and the data is stored after changing it into machine comprehensible binary language.

In this article, our focus point is to write strings to a file in Python. To write a string to a Python file, we first call the write() function on the text file object and then pass the string as a parameter to this write() function. In this quick tutorial, you will learn how to create a text file using a string, replace the original string with the new string, and display a list of strings in the text file using the for loop.

Example 1

In our first example, we have created an empty file on the desktop. In the code, we have entered the exact location of the text file that we have created. C:\Users\HP\Desktop is the path where the text file is created in our case. After that, we have written the string to the text file through the following code (as you can see in the second screenshot).

The name of the file is my_txt file. The following text is in sample_str=’Welcome to Python’. Below you can find the complete Python code (don’t forget to put a ‘r’ before your path name to avoid any path errors).

mytxt= open(r' C:\Users\HP\Desktop\myfile.txt','w')
sample_str = 'Welcome to Python'
mytxt.write(sample_str)
mytxt.close()

You will see the new text file in your given directory after running the code (tuned to your path). You can view the real string if you open the text file.

Example 2

In our second example, we are going to add a new value to the original string. Let’s see if we want to alter the string to something like this: ‘This is to overwrite the original text’. In that instance, simply make the following changes to the code. As a result, the updated Python code would be as follows:

mytxt = open(r' C:\Users\HP\Desktop\myfile.txt','w')
sample_str = “This is to overwrite the original text”
mytxt.write(sample_str)
mytxt.close()

As you can see in the following screenshot, the previous text is overwritten with the new text mentioned in the code.

Example 3

In our last example, we are going to display a list of strings in a file that we have created at the start. It is a string-based list. In that instance, you can display your list of strings in the text file using a for loop. This will print out the list in the file. By adding ‘n’ to the code, each of the strings will now be presented on a new line.

mytxt = open(r' C:\Users\HP\Desktop\myfile.txt'','w')
sample_list = ['
This is the first line','This is the second line’, 'This is the third line’]
for i in sample_list:
    mytxt.write(i + '
\n')
mytxt.close()

As you can see in the following output, that the previous text from the file is removed and a list of strings is displayed as mentioned in the code.

Conclusion

In this article, you have learned that to open a text file for appending, use the open() method with the w or a mode. Always use the close() method to close the file after you’ve finished writing it, or use them with a statement to open it. To write text into a text file, make use of the write() function.

We have explained three examples to help you understand how you can write a text into a file created in whatever location of your system (don’t forget to specify the path accurately), overwrite it, and display a list of strings. Other than strings, you can also deal with integers. The guidelines are almost the same with minor changes when you want to display or calculate integer values.



from https://ift.tt/3cBkU46

Post a Comment

0 Comments