Preview, Rewind, Re-read

Hard ⏱ 18 min 25% acceptance ★★★★☆ 4.3
Write preview_and_count(path, n) that reads the first n characters of a file, then rewinds with seek(0) and counts the total characters — returning the tuple (preview, total_chars) from a single open.

Examples

Example 1
Input
preview_and_count('note.txt', 5)  # content: 'hello world'
Output
('hello', 11)
Explanation

Five preview chars; eleven total after rewinding.

Constraints

  • Open the file exactly once.
  • Use seek to rewind.

Topics

File Handlingseek/tell

Companies

OracleIBMIntel

Hints

Hint 1

read(n) advances the position by n.

Hint 2

seek(0) returns to the start; len(read()) counts the rest.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.