Introduction
As we all know a file is a collection of text that keeps the records of any event. File handling is essential for programmers as it allows users to perform operations on files such as read, write, append, etc.
File handling in python is super easy and it is important in any programming language because it stores the text data that can be used by programmers or programs. The basic steps in file handling are open a file, perform operations and close the file.
Python treats files as text or binary. Each line of code is terminated with a special character called EOL(End Of Line) characters such as a comma, newline, etc.
Opening a file in python
To access the file or to operate on the file we need to read that file first. Python has an open( ) function that is used to open a file. For demonstration, I will be using Jupyter Notebook. A file named “student.txt” is located in the current working directory.
Syntax
open(filename, mode)
filename: Name of the file that needs to be accessed
mode: Mode in while a file is opened
Let’s take an example
file = open("student.txt","r") print(file.name)
Output
student.txt
This shows that the file is opened successfully in reading mode and can only be used for reading purposes.
Modes of opening file in python
<r> ⇒ open file in read mode only
<rb> ⇒ open file in (read + binary) mode
<r+> ⇒ open file in (read + write) mode
<rb+> ⇒ open file in (read + write + binary) mode
<w> ⇒ open file in write mode
<wb> ⇒ open file in (write + binary) mode
<w+> ⇒ open file in (read + write) mode
<wb+> ⇒ open file in (read + write + binary) mode
<a> ⇒ open file in append mode
<ab> ⇒ open file in (append + binary) mode
<a+> ⇒ open file in (append + read) mode
<ab+> ⇒ open file in (append + binary + read) mode
Closing a file in python
Once a file is opened it must be closed after operating in the file. Python has a close() function that is used to close the file.
Syntax
file_object.close()
Let’s take an example
file = open("student.txt","r") print(file.name) file.close() print(file.closed)
Output
student.txt True
Once the file is closed, we cannot perform any operations in the file.
Writing in a file
To write in a file python has a write() function. Also to perform the write operation file should be opened in write mode.
Syntax
file_object.write(string)
Let’s take an example
file = open("student.txt","w") file.write("File handling in python is super easy.") file.close()
To see the contents written in a file, we use readline() or readlines() function.
with open("student.txt") as f: print(f.readline())
Output
File handling in python is super easy.
Appending in file
Python has append() function to append contents in a file. This file should be open in append mode.
file = open("student.txt","a") file.write("File handling is important.") file.write("Programming with python is fun.") file.close()
To display the contents of the file we need to open the file in read mode
read_file = open("student.txt", "r") for line in read_file: print(line) read_file.close()
Output
File handeling in python is super easy.File handling is important.Programming with python is fun.
Renaming a file
Renaming file needs rename() function of the os module
import os os.rename("student.txt", "abc.txt")
The file named “student.txt” is changed to “abc.txt”.
with open("abc.txt", "r") as f: lines = f.readlines() for line in lines: print(line)
Output
python is easy. python is cool. Codefires is growing.
Removing a file
To remove files from the system we need to use the remove() function from the os module.
os.remove("abc.txt")
Thus, a file named “abc.txt” is removed from the system. If we want to verify it, we can do the following,
try: with open("abc.txt") as f: print(f.readlines()) except: print("File doesnot exists")
Output
File doesnot exists
Conclusion
File handling seems to be very useful for any programming language. With python, file handling is easy to perform.
Files allow keeping records of big projects that assist programmers in better management of the project. Hence, file handling is very essential in python.
Happy Learning 🙂