How to read from one file and write it into another in Python

Python comes with the open() function to handle files. We can open the files in following modes:

  • read: Opens the file in reading mode.
  • write: If the file is not present, it will create a file with the provided name and open the file in reading mode. It will overwrite the previous data.
  • append: This is the same as opening in write mode but it doesn’t overwrite the previous data.

Reading line by line

  • Open the file1, which has content in reading mode.

  • Open the file2 in writing mode.

  • Use for in loop to go through every line in file1 and write to file2.

  • Content is written to file2.

  • Close both the files.

  • We will check if the content is written into file2 or not by opening the file2 in reading mode and printing the content.

  • Close the file2.

Code

In the following code snippet, you can also check that file2.txt is created. Compare the list of before-after files in the output.

main.py
file1.txt
import os
# print files in present dir before creating file2
print("List of files before")
print(os.listdir())
print("\n")
#open file1 in reading mode
file1 = open('file1.txt', 'r')
#open file2 in writing mode
file2 = open('file2.txt','w')
#read from file1 and write to file2
for line in file1:
file2.write(line)
#close file1 and file2
file1.close()
file2.close()
#open file2 in reading mode
file2 = open('file2.txt','r')
#print the file2 content
print(file2.read())
#close the file2
file2.close()
#print the files after creating file2, check the output
print("\n")
print("List of files after")
print(os.listdir())
print("\n")

Reading with read() method

It is the same as above except that we use read() method to read the content file1.

Check line 15, where we are reading the content from file1 with file1.read() and writing it to file2.

Code

main.py
file1.txt
import os
# print files in present dir before creating file2
print("List of files before")
print(os.listdir())
print("\n")
#open file1 in reading mode
file1 = open('file1.txt', 'r')
#open file2 in writing mode
file2 = open('file2.txt','w')
#read from file1 and write to file2 using read method
file2.write(file1.read())
#close file1 and file2
file1.close()
file2.close()
#open file2 in reading mode
file2 = open('file2.txt','r')
#print the file2 content
print(file2.read())
#close the file2
file2.close()
#print the files after creating file2, check the output
print("\n")
print("List of files after")
print(os.listdir())
print("\n")

We can open files with the with keyword, so we don’t need to close files.

main.py
file1.txt
import os
# print files in present dir before creating file2
print("List of files before")
print(os.listdir())
print("\n")
#reading from file1 and writing to file2
with open("file1.txt", "r") as file1:
with open("file2.txt", "w") as file2:
file2.write(file1.read())
#reading from file1
with open("file2.txt") as file2:
print(file2.read())
#print the files after creating file2, check the output
print("\n")
print("List of files after")
print(os.listdir())
print("\n")