This assignment covers basic, intermediate, and advanced questions based on the file handling topics in Chapter 11.
Basic Questions (10)
- What is the difference between the file modes 'r', 'w', and 'a'?
- How do you open a file using a context manager?
- Write a Python code snippet to read all lines from a text file.
- How can you write a list of strings to a file?
- What Python module would you use to handle CSV files?
- How do you load a JSON file into a Python dictionary?
- How do you safely delete a file in Python?
- What does the `with open()` statement help prevent?
- Explain the purpose of the `os` module in file handling.
- How do you append new data to an existing file?
Intermediate Questions (10)
- Write a Python code example to read a CSV file and print each row as a dictionary.
- How do you convert a Python dictionary into a JSON formatted string and write it to a file?
- Explain the difference between reading a file in text mode vs binary mode.
- Describe how to handle exceptions when trying to open a non-existent file.
- Write a Python snippet to parse an XML file and extract data from a specific tag.
- What are the benefits of using the `pathlib` module over `os.path` for file paths?
- How would you read a large file without loading it entirely into memory?
- Demonstrate how to rename a file using Python.
- Write code to merge two CSV files into a single file.
- Explain how to check if a file exists before performing operations on it.
Advanced Questions (10)
- Write a Python function that reads a CSV file and converts it into a nested JSON structure.
- How can you use the `xml.etree.ElementTree` module to modify an XML file and save the changes?
- Explain the concept and usage of file buffering and its impact on file I/O performance.
- Demonstrate how to handle concurrent file access issues in Python.
- Write a script that recursively deletes all files with a specific extension in a directory and its subdirectories.
- Explain how to securely create and handle temporary files in Python.
- How would you implement a backup system that writes changes to a new file before overwriting the original?
- Write Python code to efficiently read and write very large JSON files using a streaming approach.
- Describe the difference between shallow and deep copies when dealing with file metadata objects.
- Discuss the security implications of improper file handling in Python applications and how to mitigate them.