open file on mac

Benjamin Kaplan benjamin.kaplan at case.edu
Sun Oct 10 17:44:46 EDT 2010


On Sun, Oct 10, 2010 at 5:29 PM, tinauser <tinauser at libero.it> wrote:
> On Oct 10, 6:54 am, Lawrence D'Oliveiro <l... at geek-
> central.gen.new_zealand> wrote:
>> In message
>> <d38a6549-379b-4ece-b391-904e15412... at i21g2000yqg.googlegroups.com>,
>>
>> tinauser wrote:
>> > 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?
>>
>> The fact that sys.path is not used for that.
>
> ok,then could you tell me if there is a way to have the folder of the
> module where the "open" command is?
> this module is called from C, and user could install the python code
> folder (where both the module and the file are) where he wants.
>

open uses the current working directory, which is available through
the os.getcwd() function in Python.
If you want to use a path relative to the current module, you can use
that module's __file__ attribute.
os.path.dirname(os.path.abspath(__file__))
will give you the absolute path to the directory with the current module in it.

So if you want to open the foo.txt file in the same directory as the
module, you use
os.path.join(os.path.dirname(__file__), 'foo.txt')

> thank you in advance
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list