Python
Data Analytics

File Handling in python

Venu Kumar.M

Venu Kumar.M

16 August 2024 - 0 min read

File Handling in Python

File handling in Python, python has tools to interact with data through files. File handling allows users to create or read or write the data in to the files. Files could be of any format like CSV, excel, Jason, etc..

The file handling functions in python Python has a set of built-in functions for file handling, let's see some of its essential functions,

1. open() function:

This is the basic file function to interact with files, it takes two arguments the file name and the mode we want to open the file in (e.g., "read", "write", "append" etc...)

  • read() method : This method allows users to read the entire content of a file. It is denoted by the character 'r'. Using this method files cannot be modified

Example:- Open a file named "data.txt" in read mode ("r") file = open('data.txt', 'r') content = file.read() print(content) file.close()

  • write() method: This method is used to write data to a file, It is denoted by the character 'w'. Using this method the existing files are overwritten otherwise, this creates a new file when the file doesn’t exist

Example:- #Open a file named 'data.tx' in write mode ('w') file = open('data.txt', 'w') file.write('This is some new data!') file.close()

append() method:

Similar to write() method, but this method adds new data to the end of the file without erasing existing content

Example:- #Open a file named 'data.txt' in append mode ('a') file = open('data.txt', 'a') file.write('\nHere's some additional text!') file.close()

binary method 'b’:

This method is used to handle binary files like jpg, mp4, mp3 etc.. It is denoted by the character 'b'. 'rb' – is used to read binary and 'wb' – is used to write binary

Example:-

#Open a binary file named 'image.jpg' in read binary mode ('rb') file = open('image.jpg', 'rb') image_data = file.read() file.close()

exclusive creation method 'x':

This mode is used to create a new file, It is denoted by the character 'x'

Example:- file = open('new_file.txt', 'x') file.write('This is a new file.') file.close() print('File created successfully.')

2. close() function:

Once users are done working with a file, its crucial to close the current file, we use the close() method to release resources and avoid errors.

Example:- file = open("data.txt", 'r')# Open a file

#Users file operations here (read/write) file.close()

Note:-

Append Read and write in a file with 'a+':

In 'a+' method, the new data is appended at the end of the file, '+' creates or reads the existing/new file

Read and write in a file with 'r+' & 'w+':

In r+ & w+ method, they can perform both read and write on the file, but overwrite the data from the beginning of file

Difference between 'r+' and 'w+':

In 'r+' it gives an error if the file does not exist, whereas 'w+' creates a new file if there is no file 'r+' opens the file without deleting content whereas 'w+' opens the file by deleting the contents of the file

Summary

Python provides set of tools for file handling, allowing users to interact with various data formats like CSV, Excel, and JSON etc.,. With these tools, one can perform activities like, Create new files: Start fresh with open() Read existing files: Access information using open() in 'r' mode Write data to files: Modify or create content using open() in 'w' mode (existing content will be overwritten) Append data to files: Add new information to the end of a file without erasing existing data using open() in 'a' mode Handle binary files: Work with images, audio, and other non-text data using open() with 'b' mode (e.g., 'rb' for reading binary, 'wb' for writing binary) close() function to release resources and avoid errors after file operations By understanding the different file handling methods (open(), read(), write(), append(), close()) and their modes ('r', 'w', 'a', 'b', 'x', 'r+', 'w+'), users can effectively manage their data

about the author

I'm a Data Analyst with a consulting background. Proficient in SQL, Python, and Power BI