-
Notifications
You must be signed in to change notification settings - Fork 175
/
Copy pathex20.py
executable file
·68 lines (48 loc) · 1.96 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
56
57
58
59
60
61
62
63
64
65
66
67
68
#!/bin/python3
# ex20: Functions and Files
# Import argv variables from the sys module
from sys import argv
# Assign the first and the second arguments to the two variables
script, input_file = argv
# Define a function called print_call to print the whole contents of a
# file, with one file object as formal parameter
def print_all(f):
# print the file contents
print(f.read())
# Define a function called rewind to make the file reader go back to
# the first byte of the file, with one file object as formal parameter
def rewind(f):
# make the file reader go back to the first byte of the file
f.seek(0)
# Define a function called print_a_line to print a line of the file,
# with a integer counter and a file object as formal parameters
def print_a_line(line_count, f):
# Test whether two variables are carrying the same value
print("line_count equal to current_line?:", (line_count == current_line))
# print the number and the contents of a line
print(line_count, f.readline())
# Open a file
current_file = open(input_file)
# Print "First let's print the whole file:"
print("First let's print the whole file:\n")
# call the print_all function to print the whole file
print_all(current_file)
# Print "Now let's rewind, kind of like a tape."
print("Now let's rewind, kind of like a tape.")
# Call the rewind function to go back to the beginning of the file
rewind(current_file)
# Now print three lines from the top of the file
# Print "Let's print three lines:"
print("Let's print three lines:")
# Set current line to 1
current_line = 1
# Print current line by calling print_a_line function
print_a_line(current_line, current_file)
# Set current line to 2 by adding 1
current_line = current_line + 1
# Print current line by calling print_a_line function
print_a_line(current_line, current_file)
# Set current line to 3 by adding 1
current_line = current_line + 1
# Print current line by calling print_a_line function
print_a_line(current_line, current_file)