forked from leighMichaelForrest/lpthw
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ex20.py
55 lines (42 loc) · 1.51 KB
/
ex20.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# import argv feature from sys
from sys import argv
# get the script and input_file from the command line arguments
script, input_file = argv
# function that prints the contents of a file to the screen.
# Takes a file parameter f
def print_all(f):
# write file to screen
print(f.read())
# function that returns file reader to start of file.
def rewind(f):
# go to the start of the file.
f.seek(0)
# function that prints a line number and line of text from a file.
def print_a_line(line_count, f):
# print "{line_count} {line_of_file }"
print(line_count, f.readline())
# open a file and set it to the current_file variable
current_file = open(input_file)
# write "First, let's open the whole file:\n"
print("First, let's open the whole file:\n")
# print contents of entire file.
print_all(current_file)
# write "Now let's rewind, kind of like a tape."
print("Now let's rewind, kind of like a tape.")
# move the file's reader to the beginning of the file.
rewind(current_file)
# write "Let's print three lines:"
print("Let's print three lines:")
# set current_line to one
current_line = 1
# print the current_line and the text of the current line.
# note: when operation is done, reader stays where it is.
print_a_line(current_line, current_file)
# increment current_line by one
current_line += 1
# display current_line and it's contents.
print_a_line(current_line, current_file)
# increment current_line by one
current_line += 1
# display current_line and it's contents.
print_a_line(current_line, current_file)