loading xml file?
Moderators: Omega, tillias, Mod Contributor
-
- Bounty Hunter
- Posts: 138
- Joined: Thu May 22, 2008 8:53 am
loading xml file?
some py scripts in the modules are loading xml files from the main data directory, I'm trying to do the same, the file I need to load resides in the units directory, how can I open it from my script?
-
- Star Pilot
- Posts: 4
- Joined: Wed Nov 28, 2007 3:25 am
Re: loading xml file?
Did you take a look at this already?
http://vegastrike.sourceforge.net/media ... ta_Modding
The VsWiki usually answers a lot of my scripting questions.
I personally don't know much about scripting, but from what I can gather from your question...
I assume you're talking about,
fp = open ('factions.xml','w')
I assume something similar would be used to load an xml in the units directory, substituting 'w' with 'units'.
But I'm not sure. Like I said, I don't know much about scripting...and this is out of my league.
Check the VsWiki...it can help you more then I can.
http://vegastrike.sourceforge.net/media ... ta_Modding
The VsWiki usually answers a lot of my scripting questions.
I personally don't know much about scripting, but from what I can gather from your question...
I assume you're talking about,
fp = open ('factions.xml','w')
I assume something similar would be used to load an xml in the units directory, substituting 'w' with 'units'.
But I'm not sure. Like I said, I don't know much about scripting...and this is out of my league.
Check the VsWiki...it can help you more then I can.
-
- Bounty Hunter
- Posts: 138
- Joined: Thu May 22, 2008 8:53 am
Re: loading xml file?
yes and no, I use elementtree
I was offered to use packages but I'm not sure it will work
I was offered to use packages but I'm not sure it will work
-
- Bounty Hunter
- Posts: 138
- Joined: Thu May 22, 2008 8:53 am
Re: loading xml file?
the w is for writing permissions.... not for folderdenster83 wrote:Did you take a look at this already?
http://vegastrike.sourceforge.net/media ... ta_Modding
The VsWiki usually answers a lot of my scripting questions.
I personally don't know much about scripting, but from what I can gather from your question...
I assume you're talking about,
fp = open ('factions.xml','w')
soory
I assume something similar would be used to load an xml in the units directory, substituting 'w' with 'units'.
But I'm not sure. Like I said, I don't know much about scripting...and this is out of my league.
Check the VsWiki...it can help you more then I can.
-
- Bounty Hunter
- Posts: 138
- Joined: Thu May 22, 2008 8:53 am
Re: loading xml file?
solved it using os.path.abspath()
-
- Lead Network Developer
- Posts: 2560
- Joined: Sun Jan 12, 2003 9:13 am
- Location: Palo Alto CA
- Contact:
Re: loading xml file?
yikes--not sure how to handle opening files from python. The problem with open('somefile.xml', 'r') is that it will usually be the root of the tree, but is not guaranteed to be if the current directory has changed.
How exactly are you using os.path.abspath()? Just curious if that way is portable.
How exactly are you using os.path.abspath()? Just curious if that way is portable.
-
- Bounty Hunter
- Posts: 138
- Joined: Thu May 22, 2008 8:53 am
Re: loading xml file?
not had the chance to test it within the game yet but this is what I've made:
this is based on the assumption that when calling the module, we are at the modules folder, I may be wrong in that assumption...
Code: Select all
currPath=os.path.abspath(os.path.curdir).split(os.sep)
if currPath[len(currPath)-1]=='modules':
currPath[len(currPath)-1]='units'
-
- Lead Network Developer
- Posts: 2560
- Joined: Sun Jan 12, 2003 9:13 am
- Location: Palo Alto CA
- Contact:
Re: loading xml file?
No, that assumption is correct: The modules folder for both the mod and the main data directory is always added to the sys.path.
So I guess you've just come up with the official solution on how to open files from inside python. :-p
That sort of work should really be done in the engine, but it's not exactly fun to add a new function to the python API, and I'm never exactly sure what vsfilesystem is doing. A simple rule like "one directory above the sys.path" is much easier to depend upon.
So I guess you've just come up with the official solution on how to open files from inside python. :-p
That sort of work should really be done in the engine, but it's not exactly fun to add a new function to the python API, and I'm never exactly sure what vsfilesystem is doing. A simple rule like "one directory above the sys.path" is much easier to depend upon.
-
- Bounty Hunter
- Posts: 138
- Joined: Thu May 22, 2008 8:53 am
Re: loading xml file?
I've went on and posted a path print in one of the modules that is known to be working in the game, the module resides in the modules sub folder, the output was the main tree.
I've modified it to this:
I've modified it to this:
Code: Select all
relPath=os.sep + 'units' +os.sep