Loading samples...
Loading samples...
py-sample-12
Working with files, CSV, and JSON in Python
Which mode opens file for writing?
Which mode appends to existing file?
What does read() return?
Which reads all lines into list?
What is the with statement for?
Which module handles CSV files?
What does json.dumps() do?
Which method writes string to file?
What does 'rb' mode mean?
Which checks if file exists?
Explain file modes: r, w, a, x, b, + with examples.
[3 marks]What is the advantage of using with statement for files?
[3 marks]Explain seek() and tell() methods for file positioning.
[3 marks]What is the difference between readline() and readlines()?
[3 marks]Explain CSV module: reader vs DictReader, writer vs DictWriter.
[3 marks]What are JSON and pickle modules? Compare their use cases.
[3 marks]Explain file encoding and why UTF-8 is recommended.
[3 marks]How to handle large files that don't fit in memory?
[3 marks]Explain os and shutil modules for file operations.
[3 marks]What is file buffering and flush() method?
[3 marks]Write a program to count lines, words, and characters in a file.
[5 marks]Create a program to copy one file to another (binary mode).
[5 marks]Write a CSV reader that calculates average of numeric columns.
[5 marks]Create a JSON converter that transforms CSV to JSON format.
[5 marks]Write a log file analyzer that counts ERROR vs WARNING entries.
[5 marks]Create a configuration file parser (simple key=value format).
[5 marks]Find error: f = open('data.txt') f.write('Hello') f.close()
[3 marks]Explanation:File opened in default read mode ('r'). Cannot write. Use 'w' or 'a' mode for writing.
Predict behavior: with open('test.txt', 'w') as f: f.write('Line 1') f.write('Line 2') print(f.closed)
[2 marks]Explanation:with statement automatically closes file when block exits. f.closed will be True.
Find error: f = open('data.txt', 'r') print(f.read()) print(f.read()) f.close()
[2 marks]Explanation:After first read(), cursor is at end. Second read() returns ''. Use f.seek(0) to reset.
Predict output: import json data = {'a': 1, 'b': [2, 3]} print(json.dumps(data, indent=2))
[3 marks]Explanation:dumps() converts dict to JSON string. indent=2 adds pretty formatting with 2-space indentation.
Find error: import csv with open('data.csv', 'r') as f: reader = csv.reader(f) for row in reader: print(row[5])
[3 marks]Explanation:Row may not have 6 elements. Check len(row) before accessing or use DictReader with field validation.
Predict behavior: f = open('test.txt', 'w') f.write('Hello') # Forgot to close print('Done')
[2 marks]Explanation:Without close() or flush(), data stays in buffer. Use with statement or explicit f.close().
Find error: with open('data.txt', 'rb') as f: content = f.read() print(content.upper())
[2 marks]Explanation:'rb' mode returns bytes, not string. Use .decode() or open in text mode 'r' with encoding.
Predict output: with open('test.txt', 'w') as f: print('Hello', 'World', sep='-', file=f) # File now contains
[3 marks]Explanation:print() with file= writes to file instead of stdout. sep='-' joins args. print adds newline by default.