open file on mac

T

tinauser

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?
 
L

Lawrence D'Oliveiro

In message
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.
 
T

tinauser

In message


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.

thank you in advance
 
B

Benjamin Kaplan

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')
 
T

tinauser

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')

thanks a lot!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top