Reading a file in same directory as code with relative path

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dan.j.weber@gmail.com

    Reading a file in same directory as code with relative path

    I'm trying to read an XML file in the same directory as my python code,
    using minidom:

    document = xml.dom.minidom .parse("data.xm l")

    How can I read in the file "data.xml" without knowing it's full
    path--just that it's in the same directory as my code file? Thanks for
    any help with this. I'm new to python and really liking it so far.

  • Bengt Richter

    #2
    Re: Reading a file in same directory as code with relative path

    On 17 Nov 2005 17:29:55 -0800, dan.j.weber@gma il.com wrote:
    [color=blue]
    >I'm trying to read an XML file in the same directory as my python code,
    >using minidom:
    >
    >document = xml.dom.minidom .parse("data.xm l")
    >
    >How can I read in the file "data.xml" without knowing it's full
    >path--just that it's in the same directory as my code file? Thanks for
    >any help with this. I'm new to python and really liking it so far.
    >[/color]

    ----< showmydir.py >------------
    import os
    print dir()
    print __file__
    print os.path.abspath (__file__)
    print os.path.dirname (os.path.abspat h(__file__))
    print os.path.join(os .path.dirname(o s.path.abspath( __file__)), 'data.xml')
    --------------------------------

    When run, outputs (in my directory context):

    [21:07] C:\pywk\clp>py2 4 showmydir.py
    ['__builtins__', '__doc__', '__file__', '__name__', 'os']
    showmydir.py
    C:\pywk\clp\sho wmydir.py
    C:\pywk\clp
    C:\pywk\clp\dat a.xml

    If I go somwhere else and execute it, e.g. from a sibling directory

    [21:08] C:\pywk\grammar >py24 ..\clp\showmydi r.py
    ['__builtins__', '__doc__', '__file__', '__name__', 'os']
    ...\clp\showmyd ir.py
    C:\pywk\clp\sho wmydir.py
    C:\pywk\clp
    C:\pywk\clp\dat a.xml

    (Hm, that's an interesting outcome for __file__ )

    HTH

    Regards,
    Bengt Richter

    Comment

    • Bengt Richter

      #3
      Re: Reading a file in same directory as code with relative path

      On 17 Nov 2005 17:29:55 -0800, dan.j.weber@gma il.com wrote:
      [color=blue]
      >I'm trying to read an XML file in the same directory as my python code,
      >using minidom:
      >
      >document = xml.dom.minidom .parse("data.xm l")
      >
      >How can I read in the file "data.xml" without knowing it's full
      >path--just that it's in the same directory as my code file? Thanks for
      >any help with this. I'm new to python and really liking it so far.
      >[/color]

      ----< showmydir.py >------------
      import os
      print dir()
      print __file__
      print os.path.abspath (__file__)
      print os.path.dirname (os.path.abspat h(__file__))
      print os.path.join(os .path.dirname(o s.path.abspath( __file__)), 'data.xml')
      --------------------------------

      When run, outputs (in my directory context):

      [21:07] C:\pywk\clp>py2 4 showmydir.py
      ['__builtins__', '__doc__', '__file__', '__name__', 'os']
      showmydir.py
      C:\pywk\clp\sho wmydir.py
      C:\pywk\clp
      C:\pywk\clp\dat a.xml

      If I go somwhere else and execute it, e.g. from a sibling directory

      [21:08] C:\pywk\grammar >py24 ..\clp\showmydi r.py
      ['__builtins__', '__doc__', '__file__', '__name__', 'os']
      ...\clp\showmyd ir.py
      C:\pywk\clp\sho wmydir.py
      C:\pywk\clp
      C:\pywk\clp\dat a.xml

      (Hm, that's an interesting outcome for __file__ )

      HTH

      Regards,
      Bengt Richter

      Comment

      • manuelg@gmail.com

        #4
        Re: Reading a file in same directory as code with relative path

        Answer to a similar question:



        If you want a way that will work regardless if your module is run
        interactively, imported, or just run by itself, this is a solution that
        will always work:

        :: \wherever\where ver\ (the directory your module is in,
        obviously somewhere where PYTHONPATH can
        see it)

        :::: danmodule.py (your module)

        :::: danmodule_xml\ (a subdirectory in the same directory as
        your module;
        it will only have 2 files in it)

        :::::: __init__.py (an empty textfile in danmodule_xml\,
        only here to make danmodule_xml\
        work like a package)

        :::::: data.xml (your xml file in danmodule_xml\)

        Here is the Python code for loading the file:

        # ############### # #
        import xml.dom.minidom
        import os.path
        import danmodule_xml

        xml_data_path = os.path.split(d anmodule_xml.__ file__)[0]

        xml_file_fullpa th = os.path.join(xm l_data_path, 'data.xml')

        document = xml.dom.minidom .parse(xml_file _fullpath)
        # ############### # #

        Obviously, if you have more than 1 xml files, just put them all in
        "danmodule_xml\ ".

        Comment

        • manuelg@gmail.com

          #5
          Re: Reading a file in same directory as code with relative path

          Answer to a similar question:



          If you want a way that will work regardless if your module is run
          interactively, imported, or just run by itself, this is a solution that
          will always work:

          :: \wherever\where ver\ (the directory your module is in,
          obviously somewhere where PYTHONPATH can
          see it)

          :::: danmodule.py (your module)

          :::: danmodule_xml\ (a subdirectory in the same directory as
          your module;
          it will only have 2 files in it)

          :::::: __init__.py (an empty textfile in danmodule_xml\,
          only here to make danmodule_xml\
          work like a package)

          :::::: data.xml (your xml file in danmodule_xml\)

          Here is the Python code for loading the file:

          # ############### # #
          import xml.dom.minidom
          import os.path
          import danmodule_xml

          xml_data_path = os.path.split(d anmodule_xml.__ file__)[0]

          xml_file_fullpa th = os.path.join(xm l_data_path, 'data.xml')

          document = xml.dom.minidom .parse(xml_file _fullpath)
          # ############### # #

          Obviously, if you have more than 1 xml files, just put them all in
          "danmodule_xml\ ".

          Comment

          Working...