Table of Contents
Welcome! This notebook will teach you about write the text to file in the Python Programming Language. By the end of this lab, you’ll know how to write to file and copy the file.
Writing Files
We can open a file object using the method write()
to save the text file to a list. To write the mode, argument must be set to write w. Let’s write a file Example2.txt with the line: “This is line A”
# Write line to file
with open('Example2.txt', 'w') as writefile:
writefile.write("This is line A")
We can read the file to see if it worked:
# Read file
with open('Example2.txt', 'r') as testwritefile:
print(testwritefile.read())
This is line A
Write multiple lines
We can write multiple lines:
# Write lines to file
with open('Example2.txt', 'w') as writefile:
writefile.write("This is line A\n")
writefile.write("This is line B\n")
The method .write()
works similar to the method .readline()
, except instead of reading a new line it writes a new line. The process is illustrated in the figure , the different colour coding of the grid represents a new line added to the file after each method call.
# Check whether write to file
with open('Example2.txt', 'r') as testwritefile:
print(testwritefile.read())
This is line A This is line B
By setting the mode argument to append a you can append a new line as follows:
# Write a new line to text file
with open('Example2.txt', 'a') as testwritefile:
testwritefile.write("This is line C\n")
You can verify the file has changed by running the following cell:
# Verify if the new line is in the text file
with open('Example2.txt', 'r') as testwritefile:
print(testwritefile.read())
This is line A This is line B This is line C
Write a list
We write a list to a .txt file as follows:
# Sample list of text
Lines = ["This is line A\n", "This is line B\n", "This is line C\n"]
Lines
['This is line A\n', 'This is line B\n', 'This is line C\n']
# Write the strings in the list to text file
with open('Example2.txt', 'w') as writefile:
for line in Lines:
print(line)
writefile.write(line)
This is line A This is line B This is line C
We can verify the file is written by reading it and printing out the values:
# Verify if writing to file is successfully executed
with open('Example2.txt', 'r') as testwritefile:
print(testwritefile.read())
This is line A This is line B This is line C
Append line to the file
We can again append to the file by changing the second parameter to a. This adds the code:
# Append the line to the file
with open('Example2.txt', 'a') as testwritefile:
testwritefile.write("This is line D\n")
We can see the results of appending the file:
# Verify if the appending is successfully executed
with open('Example2.txt', 'r') as testwritefile:
print(testwritefile.read())
This is line A This is line B This is line C This is line D
Copy a File
Let’s copy the file Example2.txt to the file Example3.txt:
# Copy file to another
with open('Example2.txt','r') as readfile:
with open('Example3.txt','w') as writefile:
for line in readfile:
writefile.write(line)
We can read the file to see if everything works:
# Verify if the copy is successfully executed
with open('Example3.txt','r') as testwritefile:
print(testwritefile.read())
This is line A This is line B This is line C This is line D
After reading files, we can also write data into files and save them in different file formats like .txt, .csv, .xls (for excel files) etc. Let’s take a look at some examples.
Now go to the directory to ensure the .txt file exists and contains the summary data that we wrote.