Solved

Returning source code file path at runtime

Posted on 2004-09-03
4
261 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

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…
Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
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 while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

785 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