Solved

Returning source code file path at runtime

Posted on 2004-09-03
4
254 Views
Last Modified: 2010-04-16
Can I find out the location of my .py source code when it is running? I need this so that I can save files into the same directory where the program is located. Thanks.
0
Comment
Question by:mi5
4 Comments
 
LVL 9

Accepted Solution

by:
rjkimble earned 25 total points
ID: 11976545
Try this:

import sys
print sys.argv[0]
0
 
LVL 7

Assisted Solution

by:BarryTice
BarryTice earned 25 total points
ID: 12070245
import os
currentWorkingDirectory = os.getcwd()

Depending on your platform, sys.argv[0] may or may not be useful.

sys.argv contains a list of all the command-line parameters used to invoke Python. If, for example, you use this:

python /home/barry/codeTest.py

then sys.argv[0] will either be '/home/barry/codeTest.py' or 'codeTest.py' (the reference manual says, "it is operating system dependent whether this is a full pathname or not"). One of those tells you where your file is and one doesn't.

The os.getcwd will consistently return your current working directory, but that may not always be useful either. For example, you may not actually be working out of your working directory. If I have a Python file called pathTest.py in /home/barry, but my current working directory is /home/barry/python, I can use this to invoke the script:
python ../pathTest.py

os.getcwd() would be listed as /home/barry/python, which is not where my file actually is.

You may need to experiment with some combination of these two on whatever OS you'll be running on to see if sys.argv[0] gives you a complete path, and then use that and os.getcwd() to figure out where your file is relative to the current working directory (if the file path is not absolute).

Hope this helps.

-- b.r.t.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Variable is a place holder or reserved memory locations to store any value. Which means whenever we create a variable, indirectly we are reserving some space in the memory. The interpreter assigns or allocates some space in the memory based on the d…
Strings in Python are the set of characters that, once defined, cannot be changed by any other method like replace. Even if we use the replace method it still does not modify the original string that we use, but just copies the string and then modif…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now