File Handling in Python
File Handling in Python
https://www.geeksforgeeks.org/file-handling-python/ 1/17
29/10/2024, 14:12 File Handling in Python - GeeksforGeeks
Hello world
GeeksforGeeks
123 456
Before performing any operation on the file like reading or writing, first,
we have to open that file. For this, we should use Python’s inbuilt
function open() but at the time of opening, we have to specify the mode,
which represents the purpose of the opening file.
f = open(filename, mode)
Example 1: The open command will open the Python file in the read
mode and the for loop will print each line present in the file.
Python
https://www.geeksforgeeks.org/file-handling-python/ 3/17
29/10/2024, 14:12 File Handling in Python - GeeksforGeeks
Output:
Hello world
GeeksforGeeks
123 456
Python
Output:
Hello world
GeeksforGeeks
123 456
Example 3: In this example, we will see how we can read a file using
the with statement in Python.
Python
print(data)
Output:
Hello world
GeeksforGeeks
123 456
https://www.geeksforgeeks.org/file-handling-python/ 4/17
29/10/2024, 14:12 File Handling in Python - GeeksforGeeks
Python
Output:
Hello
Example 5: We can also split lines while reading files in Python. The
split() function splits the variable when space is encountered. You can
also split using any characters as you wish.
Python
Output:
['Hello', 'world']
['GeeksforGeeks']
['123', '456']
https://www.geeksforgeeks.org/file-handling-python/ 5/17
29/10/2024, 14:12 File Handling in Python - GeeksforGeeks
Let’s see how to create a file and how the write mode works.
Example 1: In this example, we will see how the write mode and the
write() function is used to write in a file. The close() command
terminates all the resources in use and frees the system of this
particular program.
Python
Output:
Example 2: We can also use the written statement along with the
with() function.
Python
Output:
Hello World!!!
Example: For this example, we will use the Python file created in the
previous example.
Python
https://www.geeksforgeeks.org/file-handling-python/ 6/17
29/10/2024, 14:12 File Handling in Python - GeeksforGeeks
Output:
There are also various other commands in Python file handling that are
used to handle various tasks:
Python
import os
def create_file(filename):
try:
with open(filename, 'w') as f:
f.write('Hello, world!\n')
print("File " + filename + " created successfully.")
except IOError:
print("Error: could not create file " + filename)
def read_file(filename):
https://www.geeksforgeeks.org/file-handling-python/ 7/17
29/10/2024, 14:12 File Handling in Python - GeeksforGeeks
try:
with open(filename, 'r') as f:
contents = f.read()
print(contents)
except IOError:
print("Error: could not read file " + filename)
def delete_file(filename):
try:
os.remove(filename)
print("File " + filename + " deleted successfully.")
except IOError:
print("Error: could not delete file " + filename)
if __name__ == '__main__':
filename = "example.txt"
new_filename = "new_example.txt"
create_file(filename)
read_file(filename)
append_file(filename, "This is some additional text.\n")
read_file(filename)
rename_file(filename, new_filename)
read_file(new_filename)
delete_file(new_filename)
Output:
https://www.geeksforgeeks.org/file-handling-python/ 8/17
29/10/2024, 14:12 File Handling in Python - GeeksforGeeks
The four primary functions used for file handling in Python are:
https://www.geeksforgeeks.org/file-handling-python/ 9/17
29/10/2024, 14:12 File Handling in Python - GeeksforGeeks
In this example:
https://www.geeksforgeeks.org/file-handling-python/ 10/17
29/10/2024, 14:12 File Handling in Python - GeeksforGeeks
GeeksforGeeks 415
Similar Reads
https://www.geeksforgeeks.org/file-handling-python/ 11/17
29/10/2024, 14:12 File Handling in Python - GeeksforGeeks
How to save file with file name from user using Python?
Prerequisites: File Handling in PythonReading and Writing to text files in
Python Saving a file with the user's custom name can be achieved using…
5 min read
https://www.geeksforgeeks.org/file-handling-python/ 12/17
29/10/2024, 14:12 File Handling in Python - GeeksforGeeks
Python - Copy all the content of one file to another file in uppercase
In this article, we are going to write a Python program to copy all the
content of one file to another file in uppercase. In order to solve this…
2 min read
https://www.geeksforgeeks.org/file-handling-python/ 13/17
29/10/2024, 14:12 File Handling in Python - GeeksforGeeks
https://www.geeksforgeeks.org/file-handling-python/ 14/17
29/10/2024, 14:12 File Handling in Python - GeeksforGeeks
Company Explore
About Us Job-A-Thon Hiring Challenge
Legal Hack-A-Thon
Careers GfG Weekly Contest
In Media Offline Classes (Delhi/NCR)
Contact Us DSA in JAVA/C++
Advertise with us Master System Design
GFG Corporate Solution Master CP
Placement Training Program GeeksforGeeks Videos
Geeks Community
Languages DSA
Python Data Structures
Java Algorithms
C++ DSA for Beginners
PHP Basic DSA Problems
GoLang DSA Roadmap
SQL DSA Interview Questions
R Language Competitive Programming
https://www.geeksforgeeks.org/file-handling-python/ 15/17
29/10/2024, 14:12 File Handling in Python - GeeksforGeeks
Android Tutorial
https://www.geeksforgeeks.org/file-handling-python/ 16/17
29/10/2024, 14:12 File Handling in Python - GeeksforGeeks
https://www.geeksforgeeks.org/file-handling-python/ 17/17