diff --git a/kjg/python-the-hard-way/e20.py b/kjg/python-the-hard-way/e20.py new file mode 100644 index 0000000..8f7cee7 --- /dev/null +++ b/kjg/python-the-hard-way/e20.py @@ -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) diff --git a/kjg/python-the-hard-way/test.txt b/kjg/python-the-hard-way/test.txt index 6de7b8c..e97bf0c 100644 --- a/kjg/python-the-hard-way/test.txt +++ b/kjg/python-the-hard-way/test.txt @@ -1 +1,3 @@ -This is a test file. +This is line 1 +This is line 2 +This is line 3 diff --git a/kjg/python.org b/kjg/python.org index 57a1a0d..aa4fc79 100644 --- a/kjg/python.org +++ b/kjg/python.org @@ -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