Python Program to Write to File

This article is created to cover a program in Python that writes some text or content to a file, entered by user at run-time of the program.

Python Write Text to a File

The question is, write a Python program to write some text to a file. The text and the name of file, must be received by user at run-time. The program given below is its answer:

print("Enter the Name of a File: ", end="")
fileName = input()
print("Enter the Text to Write in File: ", end="")
text = input()

fileHandle = open(fileName, "w")
fileHandle.write(text)
fileHandle.close()
print("\nThe given content is written on the file successfully!")

The snapshot given below shows the sample run of above Python program, with user input myfile.txt as name of file, and I'm the content. as text to write in the file:

python program write to file

After executing the above program, with same sample run as given in above snapshot, if you open the current directory, then a file named myfile.txt will be available with the content I'm the content. written in it. Here is the snapshot of the current directory, in my case:

write text to file python program

Note - Since the file myfile.txt was not available inside my current directory. Therefore the file gets created. But if the file would be available, then the file gets overwritten.

Note - The open() method opens a file and returns as a file object.

Note - The write() method is used to write content to a file.

Note - The close() method is used to break the linkage of file from the program, using its object or handler, that was used to open the file.

Python Write to File Line by Line

This program is created to allow user to write multiple lines of text or content to a desired file in the current directory, the directory where the Python program's source code is saved.

print("Enter the Name of a File: ", end="")
fileName = input()
fileHandle = open(fileName, "w")
print("How many lines of text to write ? ", end="")
noOfLines = int(input())
print("Enter", noOfLines, "lines of text, followed by ENTER key: ", end="")

for i in range(noOfLines):
    text = input()
    fileHandle.write(text)
    fileHandle.write("\n")

fileHandle.close()
print("\nAll lines are successfully written to the file!")

print("\nWant to read (y/n) ? ", end="")
choice = input()
if choice == 'y':
    fileHandle = open(fileName, "r")
    print("\nFile contains:")
    print(fileHandle.read())
    fileHandle.close()

Sample run of above Python program, with user input codescracker.txt as name of file, 5 as total lines of text to write, and:

  1. I'm the content of first line.
  2. I'm the content of second line.
  3. I'm the content of third line.
  4. I'm the content of fourth line.
  5. I'm the content of fifth line.

as five lines of text to write to the file, and finally y as choice to see the content of file, back on the output screen, is shown in the snapshot given below:

python code write text to file

Note - The read() method is used to read the content of a file.

To learn about Python File Handling in detail, refer to its separate tutorial.

Same Program in Other Languages

Python Online Test


« Previous Program Next Program »


Liked this post? Share it!