
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. The existing file is overwritten by the new content. When you create a file in write mode and call write() function, existing data is lost and new data is written to the file.įor instance, in the previous example, we created a file and written some data to it. If you try to write a string to an existing file, be careful. N = text_file.write('Python Tutorial by TutorialKart.') write() function returns the number of bytes written to the file. But to programmatically confirm, you can use the value returned by write() function. When we run this program, a new file is created named data.txt in D: drive and the string is written to the file.

Text_file.write('Python Tutorial by TutorialKart.') The following is an example Python program in its simplest form to write string to a text file. Python – Print unique characters of a string.Python – Iterate over characters of a string.


