473,386 Members | 1,908 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,386 software developers and data experts.

Finding the path to a file

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

Jul 18 '05 #1
1 2083
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 <ov**********@yahoo.com> wrote in message news:<ma**********************************@python. org>...
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

Jul 18 '05 #2

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

Similar topics

4
by: Hal Vaughan | last post by:
I want to have a config file for my program, which means I need to know where the config file is. If I type: java myclass and it runs myclass.class, is there any way to obtain the location of...
5
by: hokiegal99 | last post by:
Hi, I have a working Python script that renames files that don't currently have PC based file extensions. For example, if there is a MS Word file that does not have '.doc' on the end of it, the...
17
by: Sean Ross | last post by:
Hi. Recently I made a small script to do some file transferring (among other things). I wanted to monitor the progress of the file transfer, so I needed to know the size of the files I was...
11
by: Fuzzyman | last post by:
What's the best, cross platform, way of finding out the directory a script is run from ? I've googled a bit, but can't get a clear answer. On sys.argv the docs say : argv is the script name...
8
by: Mr. B | last post by:
I'm writing an app where I'm trying to look for and List all specific file 'types' found. So I point to a specific start top level Folder... and I want to drill down through ALL sub folders to...
14
by: Mr. B | last post by:
I want to return the name of the drawing file (DWG) from the end of a string. The string will be of varying lengths... as well as the drawing file name itself. I could do it the long way by...
3
by: su | last post by:
Hi , I am trying to understand myself with some basic programs in python, I have written a small script to search for core files in the current dir. but when i tried to execute, it is searching the...
1
by: metaperl.etc | last post by:
In this traceback, the path to 3 different SQL Alchemy source files is a relative directory. However, no such directory is below my current working directory. This is problematic for two...
5
by: ron.longo | last post by:
Is there any way that I can find the path of the main .py file of my application? For example, I have an application with some resources which are in a subdirectory: myPythonApp.py...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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.