473,657 Members | 2,845 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.

Nov 22 '05 #1
4 6520
On 17 Nov 2005 17:29:55 -0800, da*********@gma il.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.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.


----< 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
Nov 22 '05 #2
On 17 Nov 2005 17:29:55 -0800, da*********@gma il.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.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.


----< 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
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\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\ ".

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\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\ ".

Nov 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
5451
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. I want to develop a webpage where people can send attachments that are stored on their local PC.
1
1556
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 to my package root directory (i.e. both when installed, and in my development directory?) Or to rephrase, say the data file is mypkg/data/foo.dat relative to the package root "mypkg" (the directory that holds __init__.py). Is there an enquiry...
1
3303
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 appear to work, because the base being used is the Common7\IDE directory, not my original source directory. Here is the situation: MyApp directory contains the file Form1.cs, Form1.xml, and Generic.xml. There is also a subdirectory called...
0
349
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 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.
5
4463
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 libraries. We are having a path reference issue for the depending projects; if the build is started in another directory than that of the imported project. (It's a simple cannot find project xyz.csproj, build fails.) If we place the xml file in...
12
5725
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 post my solution here. Is there a better one? Suppose you have a set of files in a directory c:\a\b and some additional files in c:\a\b\subdir. Using a Python script, you would
10
3758
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 be run as commands to a file with no extension. This allows other programs to use the command as an interface, and I can re-write the program in some other language
5
4627
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 } Despite the path exist along with the file name, the code never gets executed.
4
1940
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 in, but that didn't work. #include <fstream> #include <iostream> #include <string> #include <cstdlib>
0
8384
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8820
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8601
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7314
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5630
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1937
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1601
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.