Editor

Copy a File With Line Numbers

#563 · File handling · Beginner · 35 XP · about 7 min

Write number_lines(source, target) that copies the text file source into the file target, putting each line's number, a colon and a space in front of it. Numbers start at 1, and blank lines are numbered too.

Every line you write ends with a newline, even if the last line of source did not. If target already exists, replace what is in it. Return how many lines you wrote.

Example

If poem.txt holds:

Roses are red
Violets are blue

then number_lines('poem.txt', 'numbered.txt') returns 2, and numbered.txt holds:

1: Roses are red
2: Violets are blue

Fastest solvers

Leaderboard

First solves only, ranked by stars and then time.