In this tutorial, you will learn about Python File Handling. Before going to code and definition let’s understand the need or what is it. Think about you have a Notepad where you can write, read, edit and delete content (text or word or sentences), even notepad can open text files. So now think you have an application that has to read data from a file (excel sheet, doc file, text file, cv, etc) or add. That time you needed an open a file with a program.
Here we will learn and see the examples of how to Create, Open, Read, Write and Delete files in python, which called a Python File Handling.
The Python program that does the file handling. These are built-in functions, so you don’t need for third-party libraries.
Python File Handling
The Main function for working with files (folders) in Python is the
open() function takes two parameters first filename and another mode. The filename will be file path with name and mode as per required operation on the file.
Modes in File Handling
wwrite mode – if the file doesn’t exist create it and open it in write mode. The stream is positioned at the beginning of the file.
rread mode – (Only read the file ) is a default in open function. The stream is positioned at the beginning of the file.
aappend mode (if the file doesn’t exist create it and open it in append mode). The stream is positioned at the end of the file.
w+create a file – if it doesn’t exist and open it in write mode.
r+open an existing file in read+write mode.
a+– if it doesn’t exist and opens it in append mode.
We are using w mode to create the file. If the file doesn’t exist create it and open it.
f = open("cFile.txt", "w")
A complete tutorial about how to create a file read this tutorial: Python Create File (Empty Text File)
Assume we have the empty “testFile.txt” file, located in the same folder as Python. (if file not exist, then it will create)
Now writing the file.
‘w’ – Write mode
f = open("testFile.txt", "w")
A Detailed tutorial about Writing a File in python read this post: Python Write to Text File
Read File & Open a File
Assume we have the “testFile.txt” file, located in the same folder as Python. Reading the file use
‘r’ – Read mode
Reading the file and print() all data in the console.
f = open("testFile.txt", "r")
A complete tutorial of the Reading file in photon must read this tutorial: Python Read File | Python File Open
The python delete methods for files and folders.
os.remove()– Remove (delete) the file path.
os.rmdir()will remove an empty directory.
shutil.rmtree()will delete a directory and all its contents.
First, check whether the file or folder exists or not then only delete that file.
We assume have filed in project “cFile.txt.”.
print('File does not exists')
For all Delete methods and example with explanation follow this tutorial: Python Delete File | Remove File
Python File Handling Quiz
0 of 10 questions completed
Python File Handling to test your knowledge – ( Create, Open, Update, delete and more about in Python)
It’s for Beginners, Advanced and Experienced Programmers.
You have already completed the quiz before. Hence you can not start it again.
Quiz is loading…
You must sign in or sign up to start the quiz.
You have to finish following quiz, to start this quiz:
0 of 10 questions answered correctly
Time has elapsed
You have reached 0 of 0 points, (0)
|Table is loading|
|No data available|
Question 1 of 10
Which of the following command is used to open a file “c:\textFile.txt” in read-mode only?Correct
Question 2 of 10
Which functions use to check if a file exists?
File name = “logo”Correct
Question 3 of 10
How do you insert something on a new line in a file?Correct
Question 4 of 10
Appending to a file means adding extra data into the file.Correct
Question 5 of 10
What is the last action that must be performed on a file?Correct
Question 6 of 10
What is the data type of data read from a file?Correct
Question 7 of 10
Can you create a file without using file modes in Python?Correct
Question 8 of 10
Reading from a file often involves using…Correct
Question 9 of 10
Which of the following statements are true regarding the opening modes of a file?Correct
Question 10 of 10
Which of the following commands option right to read the entire contents of a file as a string using the file object
Note: This example (Project) is developed in PyCharm 2018.2 (Community Edition)
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
All Python File Handling Examples are in Python 3, so it may change its different from python 2 or upgraded versions.