21 Feb Python File Handling
In Python, we can easily create, read, and operate on files. Python provides methods for creating and reading a new file, reading only specific parts of a file, writing data to an already created file, updating, deleting a file, etc.
Create a file in Python
The open() method is used in Python to create a new file. Let’s see a quick example:
1 2 3 |
file = open("demo.txt") |
A parameter is used in this method to work around the existence of an already created file with the same name i.e.
Create
The following will create a file, but an error will be shown if the file already exists:
1 2 3 |
file = open("demo.txt","x" ) |
Append
The following will create a file if it does not already exist:
1 2 3 |
file = open("demo.txt","a" ) |
Write
The following will create a file it does not already exist:
1 2 3 |
file = open("demo.txt","w" ) |
Read a file in Python
To read the content of a file in Python, use the read() method. At first, open the file using the open() method and then read it. Let us see an example:
Let’s say we have a file “demo.txt”, with the following content:
1 2 3 4 5 |
Learning Python from Studyopedia! Never Stop Learning! Sharing is caring! |
The file “demo.txt” is at the following location on our system:
1 2 3 |
E:\\amit\demo.txt |
Let us now read the contents of the file:
1 2 3 4 5 6 |
# Opening already created file file = open("E:\\amit\demo.txt", "r") print("Reading the contents of the file...\n",file.read()) |
Output
1 2 3 4 5 6 7 |
Reading the contents of the file... Learning Python from Studyopedia! Never Stop Learning! Sharing is caring! |
Read some content (characters) of a Python file
We use the read() method to read the content of a file. To read only some (not all) content, use the same read() method. Specify the number of characters you want to read as a parameter.
Let us see an example to read only 10 characters from a file. We have the same “demo.txt” file as shown above:
1 2 3 4 5 6 |
# Opening already created file file = open("E:\\amit\demo.txt", "r") print("Reading the contents (10 characters) of the file...\n",file.read(10)) |
Output
1 2 3 4 5 |
Reading the contents (10 characters) of the file... Learning P |
Read specific lines from a Python file
To read specific lines, use the readline() method. To read only one line, use the method once, for two lines, use it twice and this goes on.
Let us see an example to read only the 1st two lines from a file. We have the same “demo.txt” file as shown above:
1 2 3 4 5 6 |
# Opening already created file file = open("E:\\amit\demo.txt", "r") print("Reading only two lines of the file...\n",file.readline()) |
Output
1 2 3 4 5 6 |
Reading only two lines of the file... Learning Python from Studyopedia! Never Stop Learning! |
Write contents to a File
To write contents to an already created file, use the open() method in Python. For an existing file, either overwrite the new content or append content to the end of the file:
- For overwrite, use the “w” parameter in the open() method
- For append, use the “a” parameter in the open() method
Example – Overwrite content to an existing Python file
Let’s say we have a file “demo.txt”, with the following content:
1 2 3 4 5 |
Learning Python from Studyopedia! Never Stop Learning! Sharing is caring! |
Now, we will overwrite content to an existing file “demo.txt”.
1 2 3 4 5 6 7 |
# overwrite already created file file = open("E:\\amit\demo.txt", "w") file.write("We have overwritten the existing content with this line!") file.close() |
Now, read the contents of the above file we updated:
1 2 3 4 5 |
file = open("demo.txt", "r") print(file.read()) |
Output
1 2 3 |
We have overwritten the existing content with this line! |
Example – Append content to an existing Python file
Let’s say we have a file “demo2.txt”, with the following content:
1 2 3 4 |
This is it!!! Studyopedia! Never Stop Learning! |
Now, we will append content to an existing file “demo2.txt”.
1 2 3 4 5 6 7 |
# overwrite already created file file = open("E:\\amit\demo2.txt", "w") file.write("We have added more content to the existing file!!!") file.close() |
Now, read the contents of the above file we updated:
1 2 3 4 5 |
file = open("demo2.txt", "r") print(file.read()) |
Output
1 2 3 4 |
This is it!!! Studyopedia! Never Stop Learning! We have added more content to the existing file!!! |
Delete a File in Python
To delete a file in Python, we need to import a module os:
1 2 3 |
import os |
Let’s say, we have a file demo2.txt and we need to remove it. Use the os.remove() method:
1 2 3 4 |
import os os.remove("demo2.txt") |
In this tutorial, we learned how to handle files in Python. How to create, read, and write to a file in Python. We also saw how to delete a file.
Read More
No Comments