Reading a file in same directory as code with relative path

D

dan.j.weber

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

Bengt Richter

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(__file__)), '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
 
M

manuelg

Answer to a similar question:

http://groups.google.com/group/comp.lang.python/msg/c01f292d7926f393?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\".
 

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
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top