Python - Write String to Text File
Python ? Write String to Text File
Python ? Write String to Text File
To write string to a file in Python, you can use write() function on the file object,
In this tutorial, we will learn how to write Python String to a file, with the help of some Python example programs.
Following is the step by step process to write a string to a text file.
1. Open the text file in write mode using open() function. The function returns a file object. 2. Call write() function on the file object, and pass the string to write() function as argument. 3. Once all the writing is done, close the file using close() function.
Example 1 ? Write String to Text File in Python
Following is an example Python program in its simplest form to write string to a text file.
example.py ? Python Program
#open text file text_file = open("D:/data.txt", "w") #write string to file text_file.write('Python Tutorial by TutorialKart.') #close file text_file.close()
When you run this program, a new file is created named data.txt in D: drive and the string is written to the file. But to programmatically confirm, you can use the value returned by write() function. write() function returns the number of bytes written to the file.
example.py ? Python Program
#open text file text_file = open("D:/data.txt", "w") #write string to file n = text_file.write('Python Tutorial by TutorialKart.') #close file text_file.close() print(n)
Output
Example 2 ? Write to an existing file
If you try to write a string to an existing file, be careful. When you create a file in write mode and call write() function, existing data is lost and new data is written to the file.
For instance, in the previous example, we created a file and written some data to it.
Now we shall run the following example.
example.py ? Python Program
#open text file text_file = open("D:/data.txt", "w") #write string to file n = text_file.write('Hello World!') #close file text_file.close()
The existing file is overwritten by the new content.
Note: If you would like to append data to a file, open file in append mode and then call write() function on the file object.
Conclusion
In this Python Tutorial, we learned how to write a string to a text file.
Python Programming
Python Tutorial Install Python Install Anaconda Python Python HelloWorld Program Python Variables Python Variable Data Type Conversion Python Comments
Control Statements
Python If Python If Else Python While Loop Python For Loop
Python String
Python String Methods Python String Length Python String Replace Python Split String Python Count Occurrences of Sub-String Python Sort List of Strings
Functions
Python Functions
Python Collections
Python List Python Dictionary
Advanced
Python Multithreading
Useful Resources
Python Interview Questions
................
................
In order to avoid copyright disputes, this page is only a partial summary.
To fulfill the demand for quickly locating and searching documents.
It is intelligent file search solution for home and business.
Related download
- python write string to text file
- python write csv example jeremy
- csv file 1 akshay xii a 2 abhishek xii a 3 arvind xii a 4 ravi cbse
- csv editing with python and pandas github pages
- practical file class xii computer science with python 083
- writing python libraries platanios
- c h a p ter file handling in 2 python
- reading and writing vector data with ogr utah state university
- dav public school prtap perboard i unit test ii
- data file handling worksheet solution
Related searches
- python 3 write string to file
- powershell write lines to text file
- powershell write string to file
- pandas write dataframe to text file
- python write string to file
- python write output to text file
- python write data to text file
- python write string to text file
- write to text file python
- write list to text file python
- python write string to a file
- powershell write to text file append