All IT Courses 50% Off
Python Tutorials

File Handling in Python 

Python is vastly used for file handling and operations performed in a file. It supports multiple programming paradigms which include:

  • Structured Programming
  • Object-oriented Programming
  • Functional Programming 

The vast Python standard library provides a wide range of features, including I/O management and predefined solutions to numerous execution-related issues. It makes use of dynamic typing and a garbage collector for memory management and run-time type safety checks on programmes. You can check out the Python programming training to learn more.

Python also provides the capability of functional programming languages. It has the capacity to organise things according to purely mathematical functions.  

File Handling in Python 

It provides file handling and enables users to manage files and carry out various operations, such as read and write, on them. When the data is going to be permanently kept in a file, file handling becomes an extremely crucial function. A file is a designated spot on a disc where related information is kept. Text format or Binary format are the two ways that files are handled in Python.  Python streamlines file-handling procedures and makes it simpler for users to carry out file-handling tasks. We must first open the file in order to conduct actions on it, such as reading and writing.

Type of File

Text File: Character data is typically stored in text files. In this case, test.txt

Images, video files, audio files, and other binary data are stored in binary files, which are used to store binary data.

File Name

A file path specifies where a file or folder is located within the computer system. There are two methods for specifying a file path.

  • Absolute path: This is always the root folder
  • Relative Path: A path that is relative to the program’s active working directory is called a relative path.

The whole directory list needed to find the file is included in the absolute path.

For instance, an absolute path to find the sales.txt file is /user/Pynative/data/sales.txt. The path string contains all the data required to locate the file.

The part of the filename that comes after the period (.) is known as the file’s extension, and it identifies the kind of file. Project.pdf is a PDF file in this instance.

Read File

We must first open the file in order to read or write to it. Python has the built-in function open() for this purpose.

To use the open(file_path, access_mode) function, pass the file path and access mode. It gives the file object back. Based on the access mode, this object is used to read or write the file.

The purpose of opening the file is represented by the access mode. R stands for reading, whereas W stands for writing. 

Writing a File

Use the access mode w to open a file in write mode and add material to it.

It is important for you to note that when a file is already present, the existing content is truncated, and the file handle is appended to the beginning of the file. If the indicated file doesn’t exist, a new one is made. Use the access mode to open a file in append mode if you wish to add material at the end of it. 

File Handling in Python 

Move File Pointer

The file handle position can be modified or moved to the desired location using the seek() method. The cursor indicates where in the file the data needs to be read or written.

Like the string index, the position (index) of the first character in files is zero.

File renaming

The os module in Python offers functions for handling file operations like renaming, deleting the file, etc. The operating system can be interacted with thanks to the OS module.

The rename() method is offered by the OS module and allows you to change the name of the supplied file.

Working With Bytes in File Handling

8 bits make up a byte, and each bit can either be 0 or 1. A byte can be represented in various ways, such as binary, octal, or hexadecimal. Bytes are how Python stores files on the hard drive.

A text-mode file is converted from bytes to a string object when it is opened. When a file is opened in binary mode, the contents are returned as bytes objects without being decoded.

Let’s now examine the procedures for writing bytes to a file in Python.

  • Utilise wb to launch the file in binary write mode.
  • Byte-based content specifications are required.
  • To write bytes to a binary file, use the write() function.


 There are other operations that can be performed in File handling. But these are some of the basic operations carried out on a file. Despite Python’s garbage collector, which removes unreferenced objects, it is still required to shut the file after an action. Check out the Python online training to find out more about File handling in Python.

Facebook Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Articles

Back to top button