What is the purpose of file handling in Python?

What is the purpose of file handling in Python?

a) To perform operations like reading, writing, and managing files on the filesystem
b) To format strings and numbers
c) To manage memory usage in the program
d) To handle exceptions in the code

Answer:

a) To perform operations like reading, writing, and managing files on the filesystem

Explanation:

File handling in Python refers to performing operations like reading, writing, and managing files on the filesystem. Python provides built-in functions and methods for interacting with files, making it easy to store data, retrieve information, and manipulate files directly from your Python programs.

# Example of opening and reading a file
with open("example.txt", "r") as file:
    content = file.read()
    print(content)

In this example, the open() function is used to open a file in read mode, and the read() method is used to read the file’s contents.

File handling is an essential part of many applications, allowing developers to work with data stored in external files, such as text files, CSV files, and log files.

Leave a Comment

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

Scroll to Top