Python Assignment– 7
File Handling & Persistence
Basic Questions
- Open a text file named ‘notes.txt’ in read mode using ‘open(“notes.txt”, “r”)’, print the first 20 characters, and close the file explicitly with ‘close()’.
- Use a ‘with’ statement to open ‘greeting.txt’ in write mode ‘w’, write a single line ‘Hello\n’, and confirm the file is auto-closed after the block.
- Append three lines to ‘greeting.txt’ using mode ‘a’ with one ‘write()’ call per line.
- Create a binary file ‘blob.bin’ by opening in ‘wb’ and writing 10 bytes using ‘bytes(range(10))’.
- Read an entire small text file using ‘read()’ and print its length; then reopen and read only the first 5 characters with ‘read(5)’.
- Read just the first line of ‘notes.txt’ using ‘readline()’ and print it without the trailing newline (use ‘rstrip()’).
- Read all lines from ‘notes.txt’ using ‘readlines()’ and print how many lines were returned.
- Write a list of strings to ‘lines.txt’ using ‘writelines()’, ensuring each string ends with ‘\n’.
- Demonstrate file pointer movement: open ‘notes.txt’, call ‘read(5)’, print ‘tell()’, then ‘seek(0)’ and read the first 5 characters again.
- Open a large text file and iterate line-by-line using ‘for line in file:’ to count lines without loading the whole file into memory; print the count.
- Using ‘json’ module, write a Python dictionary to ‘data.json’ with ‘json.dump’ and ‘ensure_ascii=False’.
- Read the same ‘data.json’ back into a variable using ‘json.load’ and print a specific key’s value.
- Serialize a small list to JSON text using ‘json.dumps’, print the string, then parse it back using ‘json.loads’.
- Create a simple CSV file ‘people.csv’ with header ‘name,age’ and two rows using ‘with open(…, “w”)’ and manual ‘write()’ calls.
- Read ‘people.csv’ back using ‘with open(…, “r”)’ and split each non-header line by ‘,’ to print ‘name -> age’.
- Pickle a Python dictionary to ‘cache.pkl’ using ‘pickle.dump’ in mode ‘wb’.
- Unpickle the object from ‘cache.pkl’ using ‘pickle.load’ in mode ‘rb’ and print the type of the loaded object.
- Demonstrate appending to a file without overwriting by writing one more line to ‘lines.txt’ in mode ‘a’ and then reading back the last line.
- Write text containing non-ASCII characters to ‘utf8.txt’ using ‘encoding=”utf-8″‘, then read it back and print.
- Safely check if a file is closed: open any file, print ‘file.closed’ before and after calling ‘close()’.
Intermediate Questions
- Copy the first 100 bytes from ‘source.bin’ to ‘copy.bin’ using ‘rb’ and ‘wb’ and verify by printing lengths read/written.
- Implement a chunked reader: open a large file and read in fixed-size chunks (e.g., 4096 bytes) with ‘read(size)’ inside a ‘while’ loop until empty; count chunks.
- Use ‘seek()’ to jump to byte offset 10 in ‘notes.txt’, read the next 15 characters, then use ‘tell()’ to print the current position.
- Create ‘log.txt’ and write timestamped lines in a loop using ‘a’ mode; then read the last 2 lines with ‘readlines()[-2:]’.
- Build a tiny JSON address book: write a list of dictionaries to ‘book.json’ with ‘json.dump(indent=2)’, then load and print all names.
- Update JSON in-place: load ‘book.json’, append a new entry to the list, and overwrite the file by reopening in ‘w’ and calling ‘json.dump’.
- Convert a Python object containing tuples and sets into JSON by first transforming it (e.g., sets to lists) before ‘json.dump’; write to ‘normalized.json’.
- Create ‘scores.csv’ using the ‘csv’ module: write a header and three rows via ‘csv.writer’; then read and compute the average score.
- Using only ‘open()’ and string methods (no ‘csv’ module), parse ‘scores.csv’ and build a list of dicts mapping header to value; print the list.
- Demonstrate newline handling: write lines with ‘\n’ on all platforms and open with ‘newline=””‘ when using the ‘csv’ module to avoid blank lines.
- Pickle multiple objects sequentially into ‘multi.pkl’ (e.g., a dict, a list, then a string) and unpickle them back in order; print each.
- Show that pickling is binary-only: attempt to open ‘cache.pkl’ with ‘r’ and catch the resulting error, then reopen with ‘rb’ correctly.
- Implement a function ‘tail(path, n)’ that prints the last ‘n’ lines of a text file efficiently using ‘seek()’ from the end and backward scanning in chunks.
- Measure file size without reading content by seeking to the end with ‘seek(0, 2)’ and using ‘tell()’; print the byte size.
- Merge two CSV files with the same headers by reading both and writing a combined file ‘merged.csv’ (preserve the header only once).
- Stream-copy text from stdin-like iteration: iterate a file object and write only lines containing a given keyword to ‘filtered.txt’.
- Write a binary pattern (e.g., 256 bytes cycling from 0..255) to ‘pattern.bin’ and verify by re-reading and checking ‘len’ and first/last byte values.
- Safely load JSON with error handling: try to ‘json.load’ a malformed file, catch ‘json.JSONDecodeError’, and print the error location attributes.
- Demonstrate file pointer after ‘readlines()’: read all lines, print ‘tell()’, then ‘seek(0)’ and read again to confirm reset.
- Implement a simple key–value store: append ‘key=value’ lines to ‘store.txt’ in ‘a’ mode; later, load the file and build a dictionary, ignoring malformed lines.
Advanced Questions
- Build a robust line processor that reads an arbitrarily large file and writes a transformed version to ‘out.txt’ using streaming (iterate line-by-line), replacing multiple spaces with a single space and trimming trailing spaces.
- Implement a JSON lines processor: read ‘data.jsonl’ where each line is a JSON object; for each line, ‘json.loads’, transform a field, and write back to ‘out.jsonl’ with ‘json.dumps’—handle malformed lines by skipping and logging to ‘errors.txt’.
- Create a binary file indexer: open ‘records.bin’ where each record is fixed 16 bytes; implement random access to record ‘k’ using ‘seek(k * 16)’ and print its bytes as a hex string.
- Write a CSV dialect normalizer that reads a CSV with potential extra spaces and varying line endings and rewrites it to ‘clean.csv’ using the ‘csv’ module with ‘newline=””‘ and ‘skipinitialspace=True’.
- Implement a simple append-only log with rotation: write log lines to ‘app.log’ until size exceeds a threshold (check with ‘tell()’ or ‘os.path.getsize’), then rotate to ‘app.log.1’ and continue writing.
- Build a mini JSON database: maintain a file ‘db.json’ containing a list; implement ‘add’, ‘list’, and ‘find’ operations by loading, modifying, and saving atomically (write to a temp file then replace) to avoid partial writes.
- Serialize custom objects: define a small class, pickle a list of its instances to ‘objects.pkl’, unpickle, and verify attributes; also show ‘protocol’ selection by passing ‘protocol=pickle.HIGHEST_PROTOCOL’.
- Implement safe unpickling by restricting loading: before unpickling, validate the file path and warn that ‘pickle’ is not safe for untrusted data; proceed only on a known-safe file you just wrote.
- Build a CSV-to-JSON converter that reads a headered CSV and writes an array of objects to ‘data.json’ using only core modules; ensure numeric fields are converted to ‘int’ where appropriate.
- Implement a resumable copier for large files: copy from ‘src.bin’ to ‘dst.bin’ in chunks and periodically write a checkpoint file storing the copied byte offset using ‘tell()’; on restart, ‘seek()’ to the saved offset and continue copying.