By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,640 Members | 1,102 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,640 IT Pros & Developers. It's quick & easy.

The folder a script is executed in

P: n/a
Hi,

How do I find out what folder a script is in while it is executing?
For example, for the file "C:/folder/script.py" contain the following
two lines of code -

myLocation = GetMyLocation()
print myLocation
>C:/folder
Thanks,

Aine.

Aug 21 '07 #1
Share this Question
Share on Google+
7 Replies

P: n/a
Ant
On Aug 21, 10:10 am, aine_ca...@yahoo.com wrote:
....
myLocation = GetMyLocation()
print myLocation
C:/folder
Do you mean the folder containing the script? Or the current working
directory?

If the former, then look at os.path.split(sys.argv[0])[0]
If the latter, try something like: os.path.abspath(os.curdir)

--
Ant...

http://antroy.blogspot.com/
Aug 21 '07 #2

P: n/a
Ant wrote:
Do you mean the folder containing the script? Or the current
working directory?

If the former, then look at os.path.split(sys.argv[0])[0]
test.py:
| #!/usr/bin/env python
| import sys,os
| print os.path.split(sys.argv[0])[0]

$ cd tmp
~/tmp$ ../test.py
...
~/tmp$

That's rather not what's intended. I'd try os.path.abspath(__file__)
instead.

Regards,
Björn

--
BOFH excuse #113:

Root nameservers are out of sync

Aug 21 '07 #3

P: n/a
Ant
On Aug 21, 10:47 am, Bjoern Schliessmann <usenet-
mail-0306.20.chr0n...@spamgourmet.comwrote:
Ant wrote:
....
| print os.path.split(sys.argv[0])[0]

$ cd tmp
~/tmp$ ../test.py
..
~/tmp$

That's rather not what's intended. I'd try os.path.abspath(__file__)
instead.
Fair point. On Win32 sys.argv[0] always seems to give the full path
rather than the relative path - hadn't realised it was different for
linux (?).

--
Ant...

http://antroy.blogspot.com/
Aug 21 '07 #4

P: n/a
Ant wrote:
Fair point. On Win32 sys.argv[0] always seems to give the full
path rather than the relative path
Strange.
- hadn't realised it was different for linux (?).
Yes, I used GNU/Linux for the example.

Regards,
Björn

--
BOFH excuse #148:

Insert coin for new game

Aug 21 '07 #5

P: n/a
On Aug 21, 4:10 am, aine_ca...@yahoo.com wrote:
Hi,

How do I find out what folder a script is in while it is executing?

For example, for the file "C:/folder/script.py" contain the following
two lines of code -

myLocation = GetMyLocation()
print myLocation
def GetMyLocation():
runningFile = sys.argv[0] if __name__ == "__main__" else __file__
return os.path.dirname(runningFile)
>
C:/folder

Thanks,

Aine.

Aug 21 '07 #6

P: n/a
On Aug 21, 10:23 am, Benjamin <musiccomposit...@gmail.comwrote:
On Aug 21, 4:10 am, aine_ca...@yahoo.com wrote:Hi,
How do I find out what folder a script is in while it is executing?
For example, for the file "C:/folder/script.py" contain the following
two lines of code -
myLocation = GetMyLocation()
print myLocation

def GetMyLocation():
runningFile = sys.argv[0] if __name__ == "__main__" else __file__
return os.path.dirname(runningFile)
>C:/folder
Thanks,
Aine.
As I understand it, this is one portable way to do it:

path = os.path.abspath(os.path.join(os.path.dirname(sys.a rgv[0]),
'.'))

Mike

Aug 21 '07 #7

P: n/a
On Aug 21, 10:23 am, Benjamin <musiccomposit...@gmail.comwrote:
On Aug 21, 4:10 am, aine_ca...@yahoo.com wrote:Hi,
How do I find out what folder a script is in while it is executing?
For example, for the file "C:/folder/script.py" contain the following
two lines of code -
myLocation = GetMyLocation()
print myLocation

def GetMyLocation():
runningFile = sys.argv[0] if __name__ == "__main__" else __file__
return os.path.dirname(runningFile)
never mind that above
def GetMyLocation():
if __name__ == "__main__":
runningFile = sys.argv[0] if os.path.isabs(sys.argv[0]) else
os.path.abs(sys.argv[0])
else:
runningFile = __file__
return runningFile

>

>C:/folder
Thanks,
Aine.

Aug 23 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.