Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Finding the path to a file

Reply
Thread Tools

Finding the path to a file

 
 
Todd Johnson
Guest
Posts: n/a
 
      08-11-2003
Hey,

I have been working on a program that stores
information for each user in a subdirectory of the
directory in which the script is run. So if my script
is in /home/someuser/myprogram, the config files and
such are in /home/someuser/myprogram/config. Now if I
invoke the interpreter from the same directory as the
script is in I can load and save the files because cwd
points to that directory(I used cwd to get the path
and then add /config to it to get to the proper
directory)

My problem is that if I invoke it with a command like
python /home/someuser/myprogram but I am in
/home/someuser, cwd does not point to the scripts
directory but to the directory the interpreter was
invoked from. This means I don't know how to find the
config files other than making them hard coded to a
certain directory. I don't want to hard code them
however so that the program can be contained in one
directory and easily removed and I also don't want to
have to specify where a user installs the program.

So basically, I want to know how to find the directory
my script is in, even when it is invoked from another
directory. Any help would be greatly appreciated.

Thanks in advance,
Todd A. Johnson

=====
The grace of the Lord Jesus Christ, and the love of God, and the communion of the Holy Ghost, be with you all. Amen. II Corinthians 13:14

__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com

 
Reply With Quote
 
 
 
 
Anand Pillai
Guest
Posts: n/a
 
      08-13-2003
One way to do this is to specify an environment
variable similar to PYTHONPATH for your application.
It will be the place where the program should search
for the config file.

On unix, one user would use it as follows.

Egs: % export MYCONFIGPATH="/etc:/usr/local/etc:/home/someuser/:
/home/soomeuser/program"

In your program, look for the config file first in cwd
.. If you cannot find it look up this env variable from
the shell using os.environ.get() and walk through the
paths using os.path.walk() to look for your cfg file.
I suggest os.path.walk() as it can recurse the sub-paths
too.

It reminds me, I have to add similar functionality for
harvestman.

Anand

Todd Johnson <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> Hey,
>
> I have been working on a program that stores
> information for each user in a subdirectory of the
> directory in which the script is run. So if my script
> is in /home/someuser/myprogram, the config files and
> such are in /home/someuser/myprogram/config. Now if I
> invoke the interpreter from the same directory as the
> script is in I can load and save the files because cwd
> points to that directory(I used cwd to get the path
> and then add /config to it to get to the proper
> directory)
>
> My problem is that if I invoke it with a command like
> python /home/someuser/myprogram but I am in
> /home/someuser, cwd does not point to the scripts
> directory but to the directory the interpreter was
> invoked from. This means I don't know how to find the
> config files other than making them hard coded to a
> certain directory. I don't want to hard code them
> however so that the program can be contained in one
> directory and easily removed and I also don't want to
> have to specify where a user installs the program.
>
> So basically, I want to know how to find the directory
> my script is in, even when it is invoked from another
> directory. Any help would be greatly appreciated.
>
> Thanks in advance,
> Todd A. Johnson
>
> =====
> The grace of the Lord Jesus Christ, and the love of God, and the communion of the Holy Ghost, be with you all. Amen. II Corinthians 13:14
>
> __________________________________
> Do you Yahoo!?
> Yahoo! SiteBuilder - Free, easy-to-use web site design software
> http://sitebuilder.yahoo.com

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Finding the local directory of a file in the python path are Dogue Python 1 05-18-2011 01:53 AM
Finding all the links in a Unix file/directory path freesoft12@gmail.com Perl Misc 3 05-12-2009 09:42 PM
Copying a file from path P1 to path P2 on a *remote* machine... varlagas@yahoo.com Java 1 07-13-2006 06:27 PM
Finding absolute file path on server? Paul ASP General 5 03-20-2006 03:35 AM
Finding the relative path of a file from a dir Suresh Jeevanandam Python 4 01-20-2006 01:26 PM



Advertisments