Exercise 13:
Create a program that opens file.txt. Read each line of the file and prepend it with a line number.
The contents of files.txt:
This is line one. This is line two. Finally, we are on the third and last line of the file.
Sample Output:
1: This is line one. 2: This is line two. 3: Finally, we are on the third and last line of the file.
Solution:
#!/usr/bin/env python3 with open('file.txt') as file: line_number = 1 for line in file: print('{}: {}'.format(line_number, line.rstrip())) line_number += 1
No comments:
Post a Comment