open file on mac

Brian Jones bkjones at gmail.com
Fri Oct 8 16:24:48 EDT 2010


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

> hallo, i'm sorry if the question is very stupid, but i cannot
> understand what i'm doing wrong here.
>
> i have this myModule.py
> <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/initfile.py",'r')
>            f = open("initfile.py",'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
issue.

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

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





> --
> http://mail.python.org/mailman/listinfo/python-list
>



-- 
Brian K. Jones
My Blog          http://www.protocolostomy.com
Follow me      http://twitter.com/bkjones
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20101008/833282c7/attachment.html>


More information about the Python-list mailing list