Find the path of a running .py program

L

Laszlo Nagy

Hi All!

There was a thread here, more than a month ago. It was about finding the
path of a .py file. I suggested os.abspath(__file__). Others told that
sys.argv[0] is better. I was not sure why I did not like that solution.
Now I ran into the problem again, and I now I remember what is the
problem with that. For windows service applications, it does not work.
The running program will be pythonservice.exe. Usually we want to know
where the python program (.py sources) are. So, if you ever write a
windows service in Python, make sure that you os.chdir to the correct
directory before you do anything else.

Original thread:

http://groups.google.com/group/comp...__+gandalf+argv&rnum=2&hl=hu#9763b5a8ab48b0a0

Here is a snippet that helped me to overcome the problem:

import sys,os
os.chdir(os.path.split(__file__)[0])


keywords: windows, service, startup directory, startup folder, path of a
..py program

I hope this comment will help others in the future.

Cheers,

Laszlo
 

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

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top