Solved

Returning source code file path at runtime

Posted on 2004-09-03
4
279 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

"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 …
The purpose of this article is to demonstrate how we can use conditional statements using Python.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
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…
Suggested Courses

622 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