open file on mac

Brian Jones bkjones at
Fri Oct 8 22:24:48 CEST 2010

On Fri, Oct 8, 2010 at 10:41 AM, tinauser <tinauser at> wrote:

> hallo, i'm sorry if the question is very stupid, but i cannot
> understand what i'm doing wrong here.
> i have this
> <code>
> class Starter:
>    def init(self,num):
>        print "hithere!"
>        print "the answer is ",num
>        import sys,os
>        print "path:",sys.path
>         print "bye"
>        try:
> ##            f = open("/Users/lguerrasio/_myfold/",'r')
>            f = open("",'r')
>            f.close()
>            print "huurray!"
>        except IOError:
>            print "The file does not exist, exiting gracefully"
>        print "This line will always print"
> </code>
> the module is in the same folder of initfile
> from terminal, i import sys and add to the path /Users/lguerrasio/
> _myfold/;
> then i import the module and call
> myModule.Starter().init(9)
> now,the file will be opened only if i give the full path, not if i
> give only the name of the file, although the folder is in the path.
> what am I missing?

sys.path, unless I've missed something, has nothing to do with *opening* a
file. It has to do with importing a python module, so it should have no
affect on your ability to simply open a file.

On my mac, in the interpreter, running open() on a file in the same
directory reported by os.path.abspath(os.path.curdir) succeeds without

What you might consider is doing something like
open(os.path.join(os.path.dirname(__file__), '')

That will insure that it always tries to open the file that's in the same
directory as the module trying to open it, assuming that's what you want.

If what you want is an import and not an open(), well... let us know :)


> --

Brian K. Jones
My Blog
Follow me
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the Python-list mailing list