ungleich-learning-circle/sami/learn-python-the-hard-way/ex20.py

47 lines
617 B
Python
Raw Normal View History

2020-05-22 14:35:56 +00:00
from sys import argv
script, input_file = argv
def print_all(f):
2020-05-26 09:52:28 +00:00
print(f.read())
2020-05-22 14:35:56 +00:00
def rewind(f):
2020-05-26 09:52:28 +00:00
f.seek(0)
2020-05-22 14:35:56 +00:00
def print_a_line(line_count, f):
2020-05-26 09:52:28 +00:00
print(line_count, f.readline())
2020-05-22 14:35:56 +00:00
current_file = open(input_file)
print("First let's print the whole file:\n")
print_all(current_file)
print("Now let's rewind, kind of like a tape.")
rewind(current_file)
print("Let's print three lines:")
current_line = 1
print_a_line(current_line, current_file)
current_line = current_line + 1
print_a_line(current_line, current_file)
current_line = current_line + 1
print_a_line(current_line, current_file)