Chapter 11: File Handling - Assignment

This assignment covers basic, intermediate, and advanced questions based on the file handling topics in Chapter 11.

Basic Questions (10)

  1. What is the difference between the file modes 'r', 'w', and 'a'?
  2. How do you open a file using a context manager?
  3. Write a Python code snippet to read all lines from a text file.
  4. How can you write a list of strings to a file?
  5. What Python module would you use to handle CSV files?
  6. How do you load a JSON file into a Python dictionary?
  7. How do you safely delete a file in Python?
  8. What does the `with open()` statement help prevent?
  9. Explain the purpose of the `os` module in file handling.
  10. How do you append new data to an existing file?

Intermediate Questions (10)

  1. Write a Python code example to read a CSV file and print each row as a dictionary.
  2. How do you convert a Python dictionary into a JSON formatted string and write it to a file?
  3. Explain the difference between reading a file in text mode vs binary mode.
  4. Describe how to handle exceptions when trying to open a non-existent file.
  5. Write a Python snippet to parse an XML file and extract data from a specific tag.
  6. What are the benefits of using the `pathlib` module over `os.path` for file paths?
  7. How would you read a large file without loading it entirely into memory?
  8. Demonstrate how to rename a file using Python.
  9. Write code to merge two CSV files into a single file.
  10. Explain how to check if a file exists before performing operations on it.

Advanced Questions (10)

  1. Write a Python function that reads a CSV file and converts it into a nested JSON structure.
  2. How can you use the `xml.etree.ElementTree` module to modify an XML file and save the changes?
  3. Explain the concept and usage of file buffering and its impact on file I/O performance.
  4. Demonstrate how to handle concurrent file access issues in Python.
  5. Write a script that recursively deletes all files with a specific extension in a directory and its subdirectories.
  6. Explain how to securely create and handle temporary files in Python.
  7. How would you implement a backup system that writes changes to a new file before overwriting the original?
  8. Write Python code to efficiently read and write very large JSON files using a streaming approach.
  9. Describe the difference between shallow and deep copies when dealing with file metadata objects.
  10. Discuss the security implications of improper file handling in Python applications and how to mitigate them.