1. How to write a file in Python
To write data to an existing file, we can open the file with the open()
function with 2 modes:
- “a“: Append mode. Open the file to add data, and create the file if the file does not exist.
- “w“: Write mode. Open the file to overwrite data, and create the file if the file does not exist.
Suppose we have a file named gochocit.txt
in the same folder as the Python .py
code file with the following content:
Hello all, welcome to gochocit.com!
Open the gochocit.txt
file and add more data to the file with the write()
function.
f = open("gochocit.txt", "a")
f.write("\nVery happy to see you visit gochocit.com.")
f.close()
#open and read the file after appending
f = open("gochocit.txt", "r")
print(f.read())
f.close()
Result
Hello all, welcome to gochocit.com!
Very happy to see you visit gochocit.com.
Open the gochocit.txt
file and overwrite data to the file.
f = open("gochocit.txt", "w")
f.write("Very happy to see you visit gochocit.com.")
f.close()
#open and read the file after overwriting
f = open("gochocit.txt", "r")
print(f.read())
f.close()
Result
Very happy to see you visit gochocit.com.
In the “w” mode, the new data will overwrite the entire existing data of the file. It can be understood simply that the old data in the file will be deleted entirely and only the new data will be written to the file.
Python also supports the writelines()
function to write a list of lines to the file.
lines = ['Hello all, welcome to gochocit.com!',
'Very happy to see you visit gochocit.com.',
'Hope it might be useful for you!']
f = open("gochocit.txt", "w")
f.writelines('\n'.join(lines))
f.close()
#open and read the file after overwriting
f = open("gochocit.txt", "r")
print(f.read())
f.close()
Result
Hello all, welcome to gochocit.com!
Very happy to see you visit gochocit.com.
Hope it might be useful for you!
In addition, to write each line to the file, we can use the for
loop:
lines = ['Hello all, welcome to gochocit.com!',
'Very happy to see you visit gochocit.com.',
'Hope it might be useful for you!']
f = open("gochocit.txt", "w")
for line in lines:
f.write(line)
f.write('\n')
f.close()
#open and read the file after overwriting
f = open("gochocit.txt", "r")
print(f.read())
f.close()
Result
Hello all, welcome to gochocit.com!
Very happy to see you visit gochocit.com.
Hope it might be useful for you!
2. How to create a file in Python
If a file does not exist, Python allows us to create a file with the open()
function in the following modes:
- “x“: Create mode. Create a specific file, and return an error if that file already exists.
- “a“: Append mode. Open the file to add data, and create the file if the file does not exist.
- “w“: Write mode. Open the file to overwrite data, and create the file if the file does not exist.
The following commands can help create a gochocit.txt
file:
f = open("gochocit.txt", "x")
or
f = open("gochocit.txt", "a")
or
f = open("gochocit.txt", "w")
3. How to delete a file in Python
To delete a file in Python, use the remove()
function in the os module when it is no longer needed.
import os
os.remove("gochocit.txt")
To avoid errors, it’s important to check if a file exists before attempting to delete it.
import os
if os.path.exists("gochocit.txt"):
os.remove("gochocit.txt")
else:
print("The file does not exist")