472,102 Members | 2,117 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,102 software developers and data experts.

The folder a script is executed in

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
7 1055
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
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
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
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
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
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
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.

Similar topics

6 posts views Thread by Bart Schelkens | last post: by
1 post views Thread by pnjbi | last post: by
3 posts views Thread by traceable1 | last post: by

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.