[Tutor] Opening files?

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Wed Mar 26 13:16:00 2003


> Futhermore, if I do the following:
>
> >>> myTest = open("C:test.txt", 'r')
> >>> print myTest
>
> I get this:
>
> <open file 'C:test.txt', mode 'r' at 0x0090CE18>
> >>>
>
> What is the last bit? An address of sorts?

Hi Jeff,


What you are seeing is Python's response to printing the file object
itself, and Python's default response is to print the address where that
object lives.


Actually, your program looks ok: what you have in 'myTest' is a "file
object" --- it's a thing that will respond to commands like "readline()"
or "read()".  Try doing:

    print myTest.read()

or

    print myTest.readline()

as your next step, and experiment with these "reading" commands.


By the way, if you're interested in details: The reference documentation
tells us many of the things we can do with file objects.  You can browse
through it:

    http://www.python.org/doc/lib/bltin-file-objects.html


Hope this helps!