0% found this document useful (0 votes)
31 views2 pages

File Handling Summary

File handling in Python is essential for permanent data storage and includes various file types such as text and binary files. Key operations involve opening, reading, writing, and closing files, with best practices recommending the use of 'with open()' for automatic closure and exception handling to prevent errors. The chapter also covers specific modules like 'pickle' for binary files and 'csv' for handling tabular data.

Uploaded by

jjayakrithika77
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views2 pages

File Handling Summary

File handling in Python is essential for permanent data storage and includes various file types such as text and binary files. Key operations involve opening, reading, writing, and closing files, with best practices recommending the use of 'with open()' for automatic closure and exception handling to prevent errors. The chapter also covers specific modules like 'pickle' for binary files and 'csv' for handling tabular data.

Uploaded by

jjayakrithika77
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

File Handling - Summary

File handling is a crucial concept in Python programming that allows for permanent data storage on

storage devices.

This chapter covers the different file types, operations, and practical examples.

1. **Types of Files**

- Text Files: Store data in plain text format; each line ends with a newline character (`\n`).

- Binary Files: Store data in binary format, suitable for non-text data like images or videos.

2. **File Operations**

- Opening a File: Use the `open()` function with modes like `'r'`, `'w'`, `'a'`, `'b'`, etc.

- Reading a File: Methods include `read()`, `readline()`, and `readlines()`.

- Writing to a File: Use `write()` or `writelines()`.

- Closing a File: Always close files using `close()` or the `with` statement.

3. **File Pointer**

- `tell()`: Returns the current file pointer position.

- `seek(offset, from_where)`: Moves the file pointer to a specified position.

4. **Binary File Handling**

- Use the `pickle` module to work with binary files.

- Methods:

- `pickle.dump(obj, file)`: Serialize an object.

- `pickle.load(file)`: Deserialize an object.


5. **CSV Files**

- Use the `csv` module for handling tabular data stored in CSV format.

- Methods:

- `csv.reader(file)`: Read CSV files.

- `csv.writer(file)`: Write CSV files.

6. **Exception Handling in File Operations**

- Prevent errors like `FileNotFoundError` using `try-except` blocks.

- Example:

```python

try:

file = open("data.txt", "r")

print(file.read())

except FileNotFoundError:

print("File not found.")

finally:

file.close()

```

7. **Best Practices**

- Use `with open()` to manage files as it ensures automatic closure.

- Always handle exceptions to avoid abrupt program termination.

This chapter emphasizes practical implementation with examples, helping students understand the

concept of file handling in Python.

You might also like