469,271 Members | 1,014 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

directories problem

I'm trying to write a script in python to run several other python
scripts using os.system(scriptName). Many of those other scripts have
lines in them which look something like:
doSomething('config.txt')
where config.txt is a file in the same directory as scriptName. When I
run scriptName from its own directory, it is able to find config.txt.
But when I run scriptName from another directory, it can't find the file
because it's looking in the directory of the script which called it
rather than its own directory. Is there any way around this?

Lowell
Jul 18 '05 #1
4 990
I'm not sure whether this is what Lowell is looking for, but I've been
looking at a similar problem and I would also like to see a solution for
that: can a python script know its own full path name?

Dan

"Lowell Kirsh" <lk****@cs.ubc.ca> wrote in message
news:ci**********@mughi.cs.ubc.ca...
I'm trying to write a script in python to run several other python scripts
using os.system(scriptName). Many of those other scripts have lines in
them which look something like:
doSomething('config.txt')
where config.txt is a file in the same directory as scriptName. When I run
scriptName from its own directory, it is able to find config.txt. But when
I run scriptName from another directory, it can't find the file because
it's looking in the directory of the script which called it rather than
its own directory. Is there any way around this?

Lowell

Jul 18 '05 #2
Dan Perl wrote:
I'm not sure whether this is what Lowell is looking for, but I've been
looking at a similar problem and I would also like to see a solution for
that: can a python script know its own full path name?


See near the end of this thread:
http://groups.google.ca/groups?threa...ing.google.com

Also note that scripts other than the main one can do it even more
easily: __file__, or if that's not always an absolute path,
os.path.abspath(__file__).

-Peter
Jul 18 '05 #3
Many thanks, Peter! Just like someone in the thread you are mentioning, I
have to go now and change a few scripts that I have.

And I guess that should give a solution for Lowell also. In each script
that's called with os.system( ), get the path of the script (use
os.path.split) and append the 'config.txt' file (use os.path.join).

Dan

"Peter Hansen" <pe***@engcorp.com> wrote in message
news:_6********************@powergate.ca...
Dan Perl wrote:
I'm not sure whether this is what Lowell is looking for, but I've been
looking at a similar problem and I would also like to see a solution for
that: can a python script know its own full path name?


See near the end of this thread:
http://groups.google.ca/groups?threa...ing.google.com

Also note that scripts other than the main one can do it even more
easily: __file__, or if that's not always an absolute path,
os.path.abspath(__file__).

-Peter

Jul 18 '05 #4
Thanks Peter! I'm eager to try this out. It seems like a much cleaner
solution than all the other options I've been considering.

Lowell

Peter Hansen wrote:
Dan Perl wrote:
I'm not sure whether this is what Lowell is looking for, but I've been
looking at a similar problem and I would also like to see a solution
for that: can a python script know its own full path name?

See near the end of this thread:
http://groups.google.ca/groups?threa...ing.google.com
Also note that scripts other than the main one can do it even more
easily: __file__, or if that's not always an absolute path,
os.path.abspath(__file__).

-Peter

Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Senthil | last post: by
3 posts views Thread by Stelrad Doulton | last post: by
4 posts views Thread by rn5a | last post: by
1 post views Thread by rn5a | last post: by
6 posts views Thread by =?Utf-8?B?WW9naSBXYXRjaGVy?= | last post: by
4 posts views Thread by Jeff Nyman | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.