[Tutor] New to this

Alex Hall mehgcap at gmail.com
Mon Jun 21 01:02:55 CEST 2010


On 6/20/10, Neil Thorman <neil.thorman at gmail.com> wrote:
> I'm picking this up as a hobby really, not having done any programming since
> Acorn I'm pretty much starting form scratch (and even back in the BASIC day
> I never really got to grips with files).
> This is from Alan Gauld's Learning to Program: Handling Files.
> http://www.freenetpages.co.uk/hp/alan.gauld/
>
> <http://www.freenetpages.co.uk/hp/alan.gauld/>Can I just check I'm getting
> it?
>
>
> *Menu.txt contains*
>
> *Spam & Eggs*
> *Spam & Chips*
> *Spam & Spam*
>
>>>>inp = file("menu.txt", "r")
> *What is inp? What does it now contain?*
It is now a reference to the location of the txt file. Python calls
these file "objects", where an object is just something on which you
can call functions. If you had a dog object you might call a "bark"
method; here, we have a file object, so we can see what the file is.
inp is not the file itself, just as any object is not the full
object's info but rather a pointer to where that info is. If you were
to print inp, I think you would get a memory address.
> *The following creates a list;*
> *
> *
> *
>>>>print inp.readlines()
> ['spam & eggs\n', 'spam & chips\n', 'spam & spam']
>
> but if I do it again I get:
>>>> print inp.readlines()
> []
>
> I'm baffled, why is inp now empty?
I suspect you have hit the end of the file. I rarely work with files
myself, but I believe there is a way to reset your pointer in the file
to the top; after the readlines call, that pointer is at the end of
the file.
>
> Many thanks
>
> Neil
>
> ps. I'm working in the IDLE Python Shell.
> *
> *
> *
> *
> *
> *
> *
> *
> *
>
>
> This email is confidential and intended for addressee only .
>


-- 
Have a great day,
Alex (msg sent from GMail website)
mehgcap at gmail.com; http://www.facebook.com/mehgcap


More information about the Tutor mailing list