469,645 Members | 1,120 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,645 developers. It's quick & easy.

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.xml")

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.

Nov 22 '05 #1
4 6202
On 17 Nov 2005 17:29:55 -0800, da*********@gmail.com wrote:
I'm trying to read an XML file in the same directory as my python code,
using minidom:

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

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.


----< showmydir.py >------------
import os
print dir()
print __file__
print os.path.abspath(__file__)
print os.path.dirname(os.path.abspath(__file__))
print os.path.join(os.path.dirname(os.path.abspath(__fil e__)), 'data.xml')
--------------------------------

When run, outputs (in my directory context):

[21:07] C:\pywk\clp>py24 showmydir.py
['__builtins__', '__doc__', '__file__', '__name__', 'os']
showmydir.py
C:\pywk\clp\showmydir.py
C:\pywk\clp
C:\pywk\clp\data.xml

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

[21:08] C:\pywk\grammar>py24 ..\clp\showmydir.py
['__builtins__', '__doc__', '__file__', '__name__', 'os']
...\clp\showmydir.py
C:\pywk\clp\showmydir.py
C:\pywk\clp
C:\pywk\clp\data.xml

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

HTH

Regards,
Bengt Richter
Nov 22 '05 #2
On 17 Nov 2005 17:29:55 -0800, da*********@gmail.com wrote:
I'm trying to read an XML file in the same directory as my python code,
using minidom:

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

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.


----< showmydir.py >------------
import os
print dir()
print __file__
print os.path.abspath(__file__)
print os.path.dirname(os.path.abspath(__file__))
print os.path.join(os.path.dirname(os.path.abspath(__fil e__)), 'data.xml')
--------------------------------

When run, outputs (in my directory context):

[21:07] C:\pywk\clp>py24 showmydir.py
['__builtins__', '__doc__', '__file__', '__name__', 'os']
showmydir.py
C:\pywk\clp\showmydir.py
C:\pywk\clp
C:\pywk\clp\data.xml

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

[21:08] C:\pywk\grammar>py24 ..\clp\showmydir.py
['__builtins__', '__doc__', '__file__', '__name__', 'os']
...\clp\showmydir.py
C:\pywk\clp\showmydir.py
C:\pywk\clp
C:\pywk\clp\data.xml

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

HTH

Regards,
Bengt Richter
Nov 22 '05 #3
Answer to a similar question:

http://groups.google.com/group/comp....926f393?hl=en&

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\wherever\ (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(danmodule_xml.__file__)[0]

xml_file_fullpath = os.path.join(xml_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\".

Nov 22 '05 #4
Answer to a similar question:

http://groups.google.com/group/comp....926f393?hl=en&

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\wherever\ (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(danmodule_xml.__file__)[0]

xml_file_fullpath = os.path.join(xml_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\".

Nov 22 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Alex VanderWoude | last post: by
5 posts views Thread by Al | last post: by
10 posts views Thread by Ben Finney | last post: by
5 posts views Thread by Eugene Anthony | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.