Python | File Handling

Python | File Handling

Python is a versatile and powerful programming language, with a vast range of features and libraries that make it a popular choice for a variety of tasks. One of its key strengths lies in its ability to handle file operations with ease. In this article, we’ll explore the basics of ‘.txt/csv/flat’ file operations in Python, including reading, writing, and manipulating files.

File Handling in Python

Before you can perform any file operations in Python, you need to open the file first. You can use the built-in open() function to open a file. The open() function takes two parameters:
1) Name of the file to open.
2) Mode in which to open it.
Here are the different modes:

Read mode (‘r’): This mode is used to open a file for reading only. If the file doesn’t exist, an error will be raised. This is default mode of open() function.
Here’s an example:

file = open('example.txt', 'r')
file = open('example.txt')

Write mode (‘w’): This mode is used to open a file for writing only. If the file doesn’t exist, a new file will be created. If the file already exists, it will be overwritten.
Here’s an example:

file = open('example.txt', 'w')

Append mode (‘a’): This mode is used to open a file for appending data. If the file doesn’t exist, a new file will be created. If the file already exists, new data will be added to the end of the file.
Here’s an example:

file = open('example.txt', 'a')

Reading Files in Python

Once a file is open in read mode, you can read its contents using some methods like read(), readline() and readlines().

read() method: This method reads the entire contents of a file.

file = open('example.txt', 'r')
content = file.read()
print(content)
file.close()

readline() method: This method reads a single line from the file.

file = open('example.txt', 'r')
line = file.readline()
print(line)
file.close()

readlines() method: This method reads all the lines in the file and returns them as a list.

file = open('example.txt', 'r')
lines = file.readlines()
for line in lines:
    print(line)
file.close()

Writing to Files in Python

Writing to a file is easy once you’ve opened it in write or append mode. Here are some examples:

Writing a single line to a file:

file = open('example.txt', 'w')
file.write('Hello, world!')
file.close()

Writing multiple lines to a file:

file = open('example.txt', 'w')
file.write('Line 1\n')
file.write('Line 2\n')
file.write('Line 3\n')
file.close()

Manipulating Files in Python

Python also offers several methods for manipulating files, such as renaming or deleting them. Here are some examples:

Renaming a file:

import os
os.rename('example.txt', 'newexample.txt')

Deleting a file:

import os
os.remove('newexample.txt')

Conclusion:
File operations are an essential part of any programming language, and Python provides a variety of built-in functions and modules for performing file operations. In this article, we have covered the basics of file operations in Python, including opening, reading, writing, and manipulating files. By using the functions and modules we have covered, you can easily read from and write to files, as well as perform other file operations in Python.

Leave a Reply

Your email address will not be published.