Govur University Logo
--> --> --> -->
...

Describe file handling in Python, including reading from and writing to files. Provide code examples.



File handling in Python involves performing operations on files, such as reading from and writing to them. Python provides built-in functions and methods to handle files efficiently. Let's explore the different aspects of file handling in Python, including reading from and writing to files, and provide code examples:

1. Opening and Closing Files:
To work with files in Python, you need to open them first. The `open()` function is used for this purpose. It takes two parameters: the file name and the mode (read, write, append, etc.). After performing the necessary operations on the file, it's important to close it using the `close()` method.

Example:

```
python`# Opening a file in read mode
file = open("example.txt", "r")

# Reading from the file
content = file.read()
print(content)

# Closing the file
file.close()`
```
2. Reading from Files:
Python offers multiple methods to read from files:
* `read()`: Reads the entire contents of the file as a string.
* `readline()`: Reads a single line from the file.
* `readlines()`: Reads all lines from the file and returns them as a list.

Example:

```
python`# Reading the entire file
file = open("example.txt", "r")
content = file.read()
print(content)
file.close()

# Reading line by line
file = open("example.txt", "r")
line = file.readline()
while line:
print(line)
line = file.readline()
file.close()

# Reading all lines into a list
file = open("example.txt", "r")
lines = file.readlines()
for line in lines:
print(line)
file.close()`
```
3. Writing to Files:
To write to a file, you need to open it in write mode ("w"). If the file does not exist, it will be created. If it already exists, the previous content will be overwritten. You can use the `write()` method to write a string to the file.

Example:

```
python`# Writing to a file
file = open("example.txt", "w")
file.write("Hello, world!")
file.close()

# Appending to a file
file = open("example.txt", "a")
file.write("\nThis is a new line.")
file.close()`
```
4. Context Managers and `with` Statement:
Using the `with` statement in Python is a recommended way to handle files. It automatically takes care of opening and closing the file, even if an exception occurs. It uses the concept of context managers.

Example:

```
python`# Reading from a file using a context manager
with open("example.txt", "r") as file:
content = file.read()
print(content)

# Writing to a file using a context manager
with open("example.txt", "w") as file:
file.write("Hello, world!")

# Appending to a file using a context manager
with open("example.txt", "a") as file:
file.write("\nThis is a new line.")`
```
File handling in Python provides a convenient way to work with files, read their contents, and write new data. It's important to ensure proper file handling practices, such as closing files after use or using the `with` statement to handle the files automatically. These techniques help prevent resource leaks and ensure efficient file operations.