Write a program to number the lines in a file?

1 answer

Answer

1253752

2026-04-25 22:36

+ Follow

You can number the lines in a file using a simple Python program. Here's an example:

<code class="language-python">with open('input.txt', 'r') as infile, open('output.txt', 'w') as outfile:

for line_number, line in enumerate(infile, start=1): outfile.write(f"{line_number}: {line}")

</code>

This code reads from input.txt, numbers each line, and writes the numbered lines to output.txt. The enumerate function is used to keep track of the line numbers, starting from 1.

ReportLike(0ShareFavorite

Copyright © 2026 eLLeNow.com All Rights Reserved.