Python Forum

Full Version: Exercise 20, Learn Python 3 the Hard Way
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
This code doesn't work as Zed Shaw says it should in his book. After the rewind the first call to print does it's job, sort of, (random characters before the first line of text, and spacing seems strange) but then the only thing that prints on lines two and three are the line numbers...no text follows. Here's the code:
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
print_a_line (current_line, current_file)

current_line = current_line +1
print_a_line(current_line, current_file)
I worked it out, though not sure it's very elegant. Put a "rewind current_file" between lines 27 - 29 and 30 - 32. Apparently, the file got read in line 27 and needed to be "rewound" before it could be read again in the following print command lines. Don't know how Zed Shaw got it to work the way it's written in his book.

Well, not really. Still have problems. I went back to the text file and put in three lines with numbers 1, 2, 3. The result is that when the script asks "First let's print the whole file.\n" I get the whole file OK. But when it drops down to the last print commands, after "Now let's print three lines" the first line of the text file prints three times. Still wondering how Zed did this. And I'm really enjoying talking to myself here. :-)
(Feb-25-2018, 08:55 PM)Dixon Wrote: [ -> ]Don't know how Zed Shaw got it to work the way it's written in his book.
Zed Shaw is not popular here he is a clown,so we do not support his contents at all.
Our list of free resources.
Worked for me as written in the book. Smile
(Feb-25-2018, 10:11 PM)hersh Wrote: [ -> ]Worked for me as written in the book. Smile

Thanks, that's helpful.

Did you bother to look at the code I posted? I've typed it in to Atom twice and checked for typographical errors; nothing.

And typed it in to Visual Studio with the same result.
(Feb-25-2018, 09:49 PM)snippsat Wrote: [ -> ]
(Feb-25-2018, 08:55 PM)Dixon Wrote: [ -> ]Don't know how Zed Shaw got it to work the way it's written in his book.
Zed Shaw is not popular here he is a clown,so we do not support his contents at all.
Our list of free resources.
Ok, you don't like Zed Shaw. But does it follow that you wouldn't help a forum member because of that?
(Feb-26-2018, 04:38 PM)Dixon Wrote: [ -> ]Ok, you don't like Zed Shaw. But does it follow that you wouldn't help a forum member because of that?
No it dos not mean that.
Here why if you wonder.