Practice File Operations in CS Thinking

Use these practice problems to test your method after reviewing the concept explanation and worked examples.

Quick Recap

The operations of reading data from files and writing data to files on a storage device, allowing programs to persist information beyond a single run. The standard pattern is: open the file, read from or write to it, then close the file to ensure data is saved properly.

File operations let your program save and load information โ€” like writing notes in a notebook and reading them later.

Showing a random 20 of 50 problems.

Example 1

challenge
A file contains '10\n20\n30'. The program reads all lines, converts each to int, and sums them. What is the sum, and what subtle bug could a trailing newline cause?

Example 2

medium
Name the mode that would CREATE the file if missing AND preserve existing content if present.

Example 3

medium
Which mode creates the file if it does not exist AND lets you write: 'r' or 'w'?

Example 4

medium
If you open a file with 'w', write nothing, and close it, what happens to a previously non-empty file?

Example 5

easy
A file 'data.txt' contains 'hello'. It is opened with mode 'a' and 'bye' is written. Contents now?

Example 6

medium
Trace: 'log.txt' is empty. Code: open 'a', write 'A'; close. open 'a', write 'B'; close. Final contents?

Example 7

medium
A program opens 'config.json' but never closes it, in a long-running loop. What resource problem can develop?

Example 8

medium
A CSV line reads 'Bob,42,USA'. After splitting on ',', what is the index of '42' and what is its type?

Example 9

easy
Which mode would you use to simply read a config file without changing it: 'r', 'w', or 'a'?

Example 10

medium
Write pseudocode that copies 'src.txt' to 'dst.txt'. What is the I/O pattern?

Example 11

medium
A program opens the same file 'r' twice without closing between, on a system with a low open-file limit, repeatedly in a loop. What failure mode appears?

Example 12

easy
Which of these is the safest way to make sure a file gets closed even if an error occurs: (A) call close at end, (B) use a with-block, (C) never close it?

Example 13

challenge
Trace across runs: program does open 'a' and writes a line 'run' each run. After running the program 4 times, how many lines does the file contain?

Example 14

easy
Which mode ERASES existing file content when opened: 'r', 'w', or 'a'?

Example 15

medium
Trace: 'x.txt' contains 'OLD'. open 'w', write 'NEW'; close. open 'r', read all; close. What was read?

Example 16

challenge
Two programs open 'counter.txt' simultaneously, each reads the value 5, increments, and writes 6. What well-known concurrency bug occurred, and how would you prevent it?

Example 17

easy
What are the three steps of the standard file-operation pattern (in order)?

Example 18

medium
Using `with open('f.txt','w') as fh: fh.write('hi')`, is fh closed after the block, and does it create the file if missing?

Example 19

hard
Trace: 'students.csv' has lines 'Ana,90' and 'Bo,75'. Code reads each line, splits on ',', tracks the highest score, and outputs the name. What is output?

Example 20

medium
Trace: 'n.txt' contains '5'. Code reads the text, converts to int, adds 3, writes the result back with 'w'. Final contents?