473,386 Members | 1,867 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.

how to find location of script/app and set as variable

46
hey Guys,

Can anyone recommend a way to find out the install location of a script/app and set it as a variable within that script/app?

The app just creates a bunch of folders. I want it to create the folder relative to where the script/app is installed.

Thanks!

Adam
Jun 25 '07 #1
2 1437
bartonc
6,596 Expert 4TB
hey Guys,

Can anyone recommend a way to find out the install location of a script/app and set it as a variable within that script/app?

The app just creates a bunch of folders. I want it to create the folder relative to where the script/app is installed.

Thanks!

Adam
If, by that, you mean: Is there a way to for a Python module to know its own name and directory? The answer is, of course, yes (there are several).

When a script is called (by command-line or (say) double-clicking, etc.), it gets its sys.argv[0] set to the filename of the scirpt:
Expand|Select|Wrap|Line Numbers
  1. import sys
  2. print sys.argv[0]
Another way: Ever module has a __file__ attribute:
Expand|Select|Wrap|Line Numbers
  1. print repr(__file__)  # I use repr() because the command-line I used to call the module looked exactly like the output
Jun 25 '07 #2
bartonc
6,596 Expert 4TB
If, by that, you mean: Is there a way to for a Python module to know its own name and directory? The answer is, of course, yes (there are several).

When a script is called (by command-line or (say) double-clicking, etc.), it gets its sys.argv[0] set to the filename of the scirpt:
Expand|Select|Wrap|Line Numbers
  1. import sys
  2. print sys.argv[0]
Another way: Ever module has a __file__ attribute:
Expand|Select|Wrap|Line Numbers
  1. print repr(__file__)  # I use repr() because the command-line I used to call the module looked exactly like the output
It turns out that these are dynamic (depending on the current directory when the script is run). For the absolute path of the current working directory, use:
Expand|Select|Wrap|Line Numbers
  1. import os
  2.  
  3. print os.getcwd()
Jun 25 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Marcello | last post by:
Hello I have a page of 2 frame: menu (menu.php) corpo (corpo.php) In menu.php i have: <script> function selAut(){ aut=autore.selectedIndex; aut=autore.options.value;
5
by: Phil | last post by:
hello I am a trying to find the text between a ? mark and an = sign: var theData; var begin; var thePageNo; var beginFrom; beginFrom = top.location.href.indexOf("="); begin =...
8
by: Phil Powell | last post by:
if (document.location.href.indexOf('?') >= 0) document.location.href = document.location.href.substring(0, document.location.href.indexOf('?')); if (document.location.href.indexOf('#') >= 0) {...
4
by: Frank Rizzo | last post by:
I have a batch script that is supposed to compile this script: vbc.exe /main:Form1 /target:winexe...
23
by: lwoods | last post by:
I am trying to pass some info to another page on my site. I set "session_start()" in page 1, assign a session variable to a value, then execute a "header('Location: ....')." But on the target...
1
by: wannieb | last post by:
I am hoping someone can help me in this little problem, I have a php page with dynamic menu boxes on it. SCRIPT ********* <!-- Below are arrays for the two dropdown menus and creates selected...
2
by: emily224 | last post by:
Hello, I have been trying to understand this source code, which I retreived from my online course test. I would like to know how to find the answer for the question on the test. Im sure the answer...
4
by: emily224 | last post by:
Hello, I have been trying to understand this source code, which I retreived from my online course test. I would like to know how to find the answer for the question on the test. Im sure the answer...
3
by: Abhinavnaresh | last post by:
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. ...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...

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.