petit_herisson

导航

Exercise 20 - functions and files

 

from sys import argv

script, input_file = argv

def print_all(f):
    print(f.read())
    
def rewind(f):
    f.seek(0)
    
def print_a_line(line_count, input_file):
    print(line_count, input_file.readline())
    
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)

output

D:\>python ex20.py ex20.txt
First let's print the whole file:
We can just give the function numbers directly: You have 20 cheeses! You have 30 boexes of crackers! Man that's enough for a party! Get a blanke. Or, we can use variables from our script: You have 10 cheeses! You have 50 boexes of crackers! Man that's enough for a party! Get a blanke. We can even do math inside too: You have 30 cheeses! You have 11 boexes of crackers! Man that's enough for a party! Get a blanke. And we can combine the two, variables and math: You have 110 cheeses! You have 1050 boexes of crackers! Man that's enough for a party! Get a blanke. Now let's rewind, kind of like a tape. Let's print three lines: 1 We can just give the function numbers directly: 2 You have 20 cheeses! 3 You have 30 boexes of crackers!

 

2019-09-29

04:22:43

posted on 2019-09-28 23:23  petit_herisson  阅读(96)  评论(0)    收藏  举报