Skip to content

Commit 602d5bd

Browse files
committed
Exercise 20 from LPTHW Book
Wednesday, January 31 2024 2159
1 parent 7d1df2d commit 602d5bd

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

ex20.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from sys import argv
2+
3+
script, input_file = argv
4+
5+
def print_all(f):
6+
print(f.read())
7+
8+
def rewind(f):
9+
f.seek(0)
10+
11+
def print_a_line(line_count, f):
12+
print(line_count, f.readline())
13+
14+
current_file = open(input_file)
15+
16+
print("First let's print the whole file:\n")
17+
18+
print_all(current_file)
19+
20+
print("\nNow let's rewind, kind of like a tape.")
21+
22+
rewind(current_file)
23+
24+
print("Let's print three lines:")
25+
26+
current_line = 1
27+
print_a_line(current_line, current_file)
28+
29+
current_line = current_line + 1
30+
print_a_line(current_line, current_file)
31+
32+
current_line = current_line + 1
33+
print_a_line(current_line, current_file)

test.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
"This is a test file."
1+
This is line 1
2+
This is line 2
3+
This is line 3

0 commit comments

Comments
 (0)