file open/read/name etc, not working

John Machin sjmachin at lexicon.net
Thu May 15 12:33:21 EDT 2008


On May 16, 2:12 am, globalrev <skanem... at yahoo.se> wrote:
> import os
>
> print os.path.exists('C:/Python25/myPrograms/netflix/test.txt')
> d=open('C:/Python25/myPrograms/netflix/flim.txt', 'r')

Two different paths again.

> d.readline()

This reads one line and then does absolutely nothing with it. The
Python interactive shell prints the result of each expression, which
is a Good Thing. For Python to do the same when running a script would
be a Bad Thing.

readline and readlines are old hat; instead, iterate over the file
object, like this:

for line in d:
    print line,

>
> returns true in the shell but prints no text even though the document
> contains text.
>
> d.name returns nothing, d.name() raises an error.

d.name should return the name of the file; I suspect that you again
have done nothing with it. d.name() would raise an exception because
d.name is not a method, so you can't call it.

HTH,
John



More information about the Python-list mailing list