Python File Handling
Python allows us to read, write/ create and delete files.
This process is called file handling.
The open() Function
The open() function allows us to read, create and update files.
It takes 2 parameters:
• file the file or file path to be opened
• mode the mode in which file is opened for
Python File Reading
To better explain this, let's say we have a folder named my_folder. Inside my_folder we have the following files:
• demo.txt
• main_code.py
The content of the demo.txt file is the following:
Hello World!
I love Python
Now, our goal is to read the content of the demo.txt file and then print it using the main_code.py file.
To achieve this, we will use the open() function with the "r" mode.
# this is the main_code.py file
f = open("demo.txt", "r")
x = f.read()
print(x)
When you run it by typing python main_code.py in the command-line, the following will be printed:
Hello World!
I love Python
Reading Lines
Notice that in the example above, we used the read() method to read the entire content of the demo.txt file at once.
We can also read each line using the readline() method.
# this is the main_code.py file
f = open("demo.txt", "r")
first_line
f.readline()
second_line
f.readline()
print(second_line)
print(first_line)
When you run the code above, the following will be printed:
Python Writing a File
writing/creating cannot be demonstrated using our online compiler.
In simplest terms, writing a file means modifying the content of a file or creating it if it does not exist yet.
In Python, there are 2 modes to write to a file.
• "w" overwrites content of a file, creates file if it does not exist
• "a" appends content to the end of a file, creates file if it does not exist
To better explain this, let's say we have a folder named my_folder. Inside my_folder we have the following files:
• demo.txt
• main_code.py
The content of the demo.txt file is the following:
I love Python
In this example, we will use the "w" mode which will overwrite
# this is the main_code.py file
f = open("demo.txt", "w")
f.write("I love JavaScript")
f.close()
When the above script is runned, the content of the demo.txt file will be this:
I love Python
Another example, this time we will use the "a" mode which will append or add content to the end of the file.
# this is the main_code.py file
f = open("demo.txt", "a")
f.write(" and Python")
f.close()
When the above script is runned, the content of the demo.txt file will be this:
I love Python and HTML
Exclusive Creation
If you want to prevent modifying existing files and just want to create a file, we can use excluse file creation.
- To do this, use the "x" mode of the open() function.
- The "x" mode will create a file if it does not exist yet, if the file already exists it will throw an error.
EXCLUSIVE creations
If you want to prevent modifying existing files and just want to create a file, we can use excluse file creation.
- To do this, use the "x" mode of the open() function.
- The "x" mode will create a file if it does not exist yet, if the file already exists it will throw an error.
- this is the main_code.py file
f = open("notes.txt", "x")
When the above script is runned, an empty file named notes.txt will be created.
Python Deleting a File
To delete files, use the os module.
import os
To better explain this, let's say we have a folder named my_folder.
Inside my_folder we have the following:
• demo.txt
• my_photos_folder
• main_code.py
Deleting a File
To delete a file, use the os.remove() method.
# this is the main_code.py file import os
os.remove("demo.txt")
Deleting a Folder
To delete a folder, use the os.rmdir() method.
# this is the main_code.py file import os
os.rmdir("my_photos_folder")
os.rmdir("my_photos_folder")
Checking if File Exists
Deleting a file that does not exist will throw an error. To prevent this, we can first check whether the file exists or not using the os.path.exists() method.
# this is the main_code.py file import os
file_path = "demo.txt"
if os.path.exists(file_path):
os.remove(file_path)
else:
print("Oops, the file doesn't open")