open file on mac

Discussion in 'Python' started by tinauser, Oct 8, 2010.

  1. tinauser

    tinauser Guest

    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?
    tinauser, Oct 8, 2010
    #1
    1. Advertising

  2. In message
    <>,
    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.
    Lawrence D'Oliveiro, Oct 10, 2010
    #2
    1. Advertising

  3. tinauser

    tinauser Guest

    On Oct 10, 6:54 am, Lawrence D'Oliveiro <l...@geek-
    central.gen.new_zealand> wrote:
    > In message
    > <>,
    >
    > 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.

    thank you in advance
    tinauser, Oct 10, 2010
    #3
  4. On Sun, Oct 10, 2010 at 5:29 PM, tinauser <> wrote:
    > On Oct 10, 6:54 am, Lawrence D'Oliveiro <l...@geek-
    > central.gen.new_zealand> wrote:
    >> In message
    >> <>,
    >>
    >> 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
    >
    Benjamin Kaplan, Oct 10, 2010
    #4
  5. tinauser

    tinauser Guest

    On Oct 10, 11:44 pm, Benjamin Kaplan <> wrote:
    > On Sun, Oct 10, 2010 at 5:29 PM, tinauser <> wrote:
    > > On Oct 10, 6:54 am, Lawrence D'Oliveiro <l...@geek-
    > > central.gen.new_zealand> wrote:
    > >> In message
    > >> <>,

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

    >
    >


    thanks a lot!
    tinauser, Oct 11, 2010
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JScoobyCed

    Open a file - Mac

    JScoobyCed, Jun 24, 2005, in forum: Java
    Replies:
    2
    Views:
    4,575
    Steve W. Jackson
    Jun 24, 2005
  2. Replies:
    1
    Views:
    244
    Tommy Nordgren
    Dec 23, 2008
  3. tinauser

    open file on mac

    tinauser, Oct 8, 2010, in forum: Python
    Replies:
    2
    Views:
    199
    Emile van Sebille
    Oct 8, 2010
  4. Schüle Daniel

    File::open and File.open

    Schüle Daniel, Oct 23, 2006, in forum: Ruby
    Replies:
    4
    Views:
    180
    Rick DeNatale
    Oct 24, 2006
  5. Ryan Westerberg

    Using ruby to open an Excel file on a mac

    Ryan Westerberg, Jul 8, 2009, in forum: Ruby
    Replies:
    5
    Views:
    200
    Matt Neuburg
    Jul 9, 2009
Loading...

Share This Page