Python Assignment– 7

File Handling & Persistence

Basic Questions

  1. 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()’.
  2. 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.
  3. Append three lines to ‘greeting.txt’ using mode ‘a’ with one ‘write()’ call per line.
  4. Create a binary file ‘blob.bin’ by opening in ‘wb’ and writing 10 bytes using ‘bytes(range(10))’.
  5. Read an entire small text file using ‘read()’ and print its length; then reopen and read only the first 5 characters with ‘read(5)’.
  6. Read just the first line of ‘notes.txt’ using ‘readline()’ and print it without the trailing newline (use ‘rstrip()’).
  7. Read all lines from ‘notes.txt’ using ‘readlines()’ and print how many lines were returned.
  8. Write a list of strings to ‘lines.txt’ using ‘writelines()’, ensuring each string ends with ‘\n’.
  9. Demonstrate file pointer movement: open ‘notes.txt’, call ‘read(5)’, print ‘tell()’, then ‘seek(0)’ and read the first 5 characters again.
  10. 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.
  11. Using ‘json’ module, write a Python dictionary to ‘data.json’ with ‘json.dump’ and ‘ensure_ascii=False’.
  12. Read the same ‘data.json’ back into a variable using ‘json.load’ and print a specific key’s value.
  13. Serialize a small list to JSON text using ‘json.dumps’, print the string, then parse it back using ‘json.loads’.
  14. Create a simple CSV file ‘people.csv’ with header ‘name,age’ and two rows using ‘with open(…, “w”)’ and manual ‘write()’ calls.
  15. Read ‘people.csv’ back using ‘with open(…, “r”)’ and split each non-header line by ‘,’ to print ‘name -> age’.
  16. Pickle a Python dictionary to ‘cache.pkl’ using ‘pickle.dump’ in mode ‘wb’.
  17. Unpickle the object from ‘cache.pkl’ using ‘pickle.load’ in mode ‘rb’ and print the type of the loaded object.
  18. 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.
  19. Write text containing non-ASCII characters to ‘utf8.txt’ using ‘encoding=”utf-8″‘, then read it back and print.
  20. Safely check if a file is closed: open any file, print ‘file.closed’ before and after calling ‘close()’.

Intermediate Questions

  1. Copy the first 100 bytes from ‘source.bin’ to ‘copy.bin’ using ‘rb’ and ‘wb’ and verify by printing lengths read/written.
  2. 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.
  3. Use ‘seek()’ to jump to byte offset 10 in ‘notes.txt’, read the next 15 characters, then use ‘tell()’ to print the current position.
  4. Create ‘log.txt’ and write timestamped lines in a loop using ‘a’ mode; then read the last 2 lines with ‘readlines()[-2:]’.
  5. 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.
  6. 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’.
  7. 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’.
  8. Create ‘scores.csv’ using the ‘csv’ module: write a header and three rows via ‘csv.writer’; then read and compute the average score.
  9. 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.
  10. Demonstrate newline handling: write lines with ‘\n’ on all platforms and open with ‘newline=””‘ when using the ‘csv’ module to avoid blank lines.
  11. Pickle multiple objects sequentially into ‘multi.pkl’ (e.g., a dict, a list, then a string) and unpickle them back in order; print each.
  12. Show that pickling is binary-only: attempt to open ‘cache.pkl’ with ‘r’ and catch the resulting error, then reopen with ‘rb’ correctly.
  13. 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.
  14. Measure file size without reading content by seeking to the end with ‘seek(0, 2)’ and using ‘tell()’; print the byte size.
  15. Merge two CSV files with the same headers by reading both and writing a combined file ‘merged.csv’ (preserve the header only once).
  16. Stream-copy text from stdin-like iteration: iterate a file object and write only lines containing a given keyword to ‘filtered.txt’.
  17. 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.
  18. Safely load JSON with error handling: try to ‘json.load’ a malformed file, catch ‘json.JSONDecodeError’, and print the error location attributes.
  19. Demonstrate file pointer after ‘readlines()’: read all lines, print ‘tell()’, then ‘seek(0)’ and read again to confirm reset.
  20. 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

  1. 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.
  2. 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’.
  3. 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.
  4. 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’.
  5. 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.
  6. 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.
  7. 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’.
  8. 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.
  9. 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.
  10. 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.