[Python #3] create e20.py
This commit is contained in:
parent
fad98b2be3
commit
991779bc67
3 changed files with 40 additions and 1 deletions
35
kjg/python-the-hard-way/e20.py
Normal file
35
kjg/python-the-hard-way/e20.py
Normal file
|
@ -0,0 +1,35 @@
|
|||
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, f):
|
||||
print(line_count, f.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
|
||||
current_line += 1
|
||||
print_a_line(current_line, current_file)
|
||||
|
||||
#current_line = current_line + 1
|
||||
current_line += 1
|
||||
print_a_line(current_line, current_file)
|
|
@ -1 +1,3 @@
|
|||
This is a test file.
|
||||
This is line 1
|
||||
This is line 2
|
||||
This is line 3
|
||||
|
|
|
@ -92,3 +92,5 @@ How to copy file
|
|||
How to use function
|
||||
**** ex19
|
||||
How to use function and variables
|
||||
**** ex20
|
||||
How to use function with file
|
||||
|
|
Loading…
Reference in a new issue