Thursday, April 30, 2026

Python Tutorial for Beginners: File Handling

We have learned how to create and use variables in Python. However, this is a temporary arrangement; they cease to exist once the program finishes executing. Python provides the capability to store data permanently, allowing it to be used even after the program has terminated. This is known as 'file handling'.

For example: Storing Records, Read file data, Store notes etc.

file handling

What is File Handling in Python?

"File Handling" is a concept used to read, write and modify file in Python language. With this, we can:

  • Create a File 
  • Read a File
  • Write data to a File
  • Update a File
There are several methods and fuctions avaliable in file handling like: open(), write(), read() etc.

File Mode

Python file handling has multiple modes that serves the specific purpose.

ModeMeaningBehavior
"r"ReadOpen a file only for reading. File must exist.
"w"WriteOpen a file for writing. Creates a new file or overwrites an existing file.
"a"AppendOpen a file to add data at the end. Existing content is preserved.
"x"Exclusive creationCreate a new file. Fails if the file already exists.
"rb"Read BinaryRead a file in binary mode (images, videos).
"wb"Write BinaryWrite a file in binary mode.
"r+"Read & WriteOpen a file for reading and writing. File must exist.
"w+"Write & ReadOpen a file for reading and writing. Overwrites existing file.
"a+"Append & ReadOpen a file for reading and appending. Adds new content at the end.

Creating a New File

Create new file using python code.
Example
file = open("newfile.text", "x")
file.close()

Opening a File

To open any file, we can use open() function.
Note: "r" mode does not create a file.
Syntax
file = open("filename.txt", "mode")
Example
file = open("Python.text", "r")  // Python.txt - file name, r - read mode
print(file.read())

Read a File

To read any file, we can use read() function.
Example
file = open("Python.text", "r")
print(file.read())
file.close()
Read one line
Example
file = open("Python.text", "r")
print(file.readline())
file.close()
Read all lines
Example
file = open("Python.text", "r")
print(file.readlines())
file.close()

Writing a File

The "w" mode is used to overwrite an existing file; that is, no old data remains in it. If the file does not exist, it will create a new file.
Example
file = open("note.txt", "w")
file.write("Python Programming")
file.close()
example1

Appeding Data

With the help of the "a" mode, we can add new lines without deleting the existing data. If the file does not exist, it also creates it.
Example
file = open("note.text", "a")
file.write("\nLearn from Scratch")
file.close
example 2

Closing File

Always close files after using them.

file.close()
This saves resources.
Use "With" statment
This statement helps us to automatically close the file after execution
Example
with open("note.text", "r") as file:
    print(file.read())

Save User Input

We can also save the user input direct in our text file.
Example
name = input("Enter your name: ")
with open("note.text", "w")
    file.write("name +\n")
user input

Practice Excercise

Task 1: Create a file and write your name into it.
Task 2: Read data from a file.
Task 3: Append your city name to an existing file.
Task 4: Store 5 student names in a file.

Conclusion

This covers the basics of file handling at an introductory level. Practice these exercises and study this chapter in depth, as the concept of file handling enables applications to store data permanently. Next Chapter: OOPs in Python

Happy learning!

Practice Your Code Below

🐍 Python Playground

No comments:

Post a Comment

Featured Post

Python OOPs Advanced: Class Variables and Instance Variables

We have learned how to create classes and objects . Now, we will explore class variables and instance variables in Python, including how the...