473,385 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 6481
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Dave Smithz | last post by:
Hi There, I have a PHP script that sends an email with attachment and works great when provided the path to the file to send. However this file needs to be on the same server as the script. ...
1
by: Parzival | last post by:
I have a package that includes some data files. I am planning to use a distutils setup script to install the package. How can I compute a path name for such a data file that will always be relative...
1
by: Alex VanderWoude | last post by:
I am trying to <include> some text into an XML documentation topic, but that text is stored in a file that is in a different directory than the "current" XML file. Using a relative path does not...
0
by: dan.j.weber | last post by:
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...
5
by: Al | last post by:
Hi all We have created a xml file that imports a single project using the Import element. This project compiles to a class library, but has references to two other projects that are also class...
12
by: pac | last post by:
I'm preparing to distribute a Windows XP Python program and some ancillary files, and I wanted to put everything in a .ZIP archive. It proved to be inordinately difficult and I thought I would...
10
by: Ben Finney | last post by:
Howdy all, Question: I have Python modules named without '.py' as the extension, and I'd like to be able to import them. How can I do that? Background: On Unix, I write programs intended to...
5
by: Eugene Anthony | last post by:
ds1.Tables.Rows.ItemArray.GetValue(0).ToString() contains the string path: images/5/Video1/qbert.flv if (File.Exists(ds1.Tables.Rows.ItemArray.GetValue(0).ToString())) { //code to execute } ...
4
by: noobles | last post by:
I'm trying to make a command line parser, but I'm not exactly sure where to put my file.txt file so that this thing can read it. The most logical place seemed to be the directory the project was...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.