The folder a script is executed in

A

aine_canby

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

Thanks,

Aine.
 
A

Ant

On Aug 21, 10:10 am, (e-mail address removed) wrote:
....
myLocation = GetMyLocation()
print myLocation

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)
 
A

aine_canby

On Aug 21, 10:10 am, (e-mail address removed) wrote:
...
myLocation = GetMyLocation()
print myLocation

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)

The following code -

import os
import sys
print os.getcwd() + "."
print os.path.split(sys.argv[0])[0] + "."

gives me -

C:\Documents and Settings\me\Desktop.
..

Im running Python 2.2 on NT
 
A

Ant

On Aug 21, 10:10 am, (e-mail address removed) 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]
If the latter, try something like: os.path.abspath(os.curdir)
http://antroy.blogspot.com/

The following code -

import os
import sys
print os.getcwd() + "."
print os.path.split(sys.argv[0])[0] + "."

gives me -

C:\Documents and Settings\me\Desktop.
.

Which looks correct. You've executed the code in an interactive
session launched from a desktop shortcut at a guess. You won't get the
location of the script unless you are actually running a script :).
Of course you'll get a current working directory however you are
running python.
 
B

Bjoern Schliessmann

Ant said:
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
 
A

Ant

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 (?).
 
B

Bjoern Schliessmann

Ant said:
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
 
B

Benjamin

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)
 
K

kyosohma

On Aug 21, 4:10 am, (e-mail address removed) 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)



As I understand it, this is one portable way to do it:

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

Mike
 
B

Benjamin

On Aug 21, 4:10 am, (e-mail address removed) 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,273
Latest member
DamonShoem

Latest Threads

Top