Solved

Returning source code file path at runtime

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

"The time has come," the Walrus said, "To talk of many things: Of sets--and lists--and dictionaries-- Of variable kinks-- And why you see it changing not-- And why so strange are strings." This part describes how variables and references (see …
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…

757 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

23 Experts available now in Live!

Get 1:1 Help Now