471,325 Members | 1,568 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

The file executing

How does one get the path to the file currently executing (not the
cwd). Thank you

Jul 3 '07 #1
5 1071
On Jul 3, 9:40 am, Benjamin <musiccomposit...@gmail.comwrote:
How does one get the path to the file currently executing (not the
cwd). Thank you
os.path.dirname(sys.argv[0])

Jul 3 '07 #2
On Jul 2, 9:47 pm, Justin Ezequiel <justin.mailingli...@gmail.com>
wrote:
On Jul 3, 9:40 am, Benjamin <musiccomposit...@gmail.comwrote:
How does one get the path to the file currently executing (not the
cwd). Thank you

os.path.dirname(sys.argv[0])
The returns the file that was called first, but not the one currently
executing...

Jul 3 '07 #3
On Jul 3, 8:56 am, Sebastian Wiesner <basti.wies...@gmx.netwrote:
[ Benjamin <musiccomposit...@gmail.com]
On Jul 2, 9:47 pm, Justin Ezequiel <justin.mailingli...@gmail.com>
wrote:
On Jul 3, 9:40 am, Benjamin <musiccomposit...@gmail.comwrote:
How does one get the path to the file currently executing (not the
cwd). Thank you
os.path.dirname(sys.argv[0])
The returns the file that was called first, but not the one currently
executing...

Use __file__ instead of sys.argv[0]
So:
if __name__ == "main":
currentDir = os.path.dirname(sys.argv[0])
else:
currentDir = os.path.dirname(__file__)
>
--
Freedom is always the freedom of dissenters.
(Rosa Luxemburg)

signature.asc
1KDownload

Jul 6 '07 #4
En Fri, 06 Jul 2007 17:15:22 -0300, Benjamin <mu**************@gmail.com>
escribió:
How does one get the path to the file currently executing (not the
cwd). Thank you
So:
if __name__ == "main":
currentDir = os.path.dirname(sys.argv[0])
else:
currentDir = os.path.dirname(__file__)
I think you meant to test for "__main__", but anyway, this should be
enough:
currentDir = os.path.dirname(__file__)
or perhaps os.path.dirname(os.path.abspath(__file__)). Try to determine
that early in your program because __file__ may contain a relative path
(and will give a wrong result after changing the current directory).

--
Gabriel Genellina

Jul 9 '07 #5
On Jul 9, 6:42 am, "Gabriel Genellina" <gagsl-...@yahoo.com.arwrote:
En Fri, 06 Jul 2007 17:15:22 -0300, Benjamin <musiccomposit...@gmail.com
escribió:
How does one get the path to the file currently executing (not the
cwd). Thank you
So:
if __name__ == "main":
currentDir = os.path.dirname(sys.argv[0])
else:
currentDir = os.path.dirname(__file__)

I think you meant to test for "__main__", but anyway, this should be
Yes
enough:
currentDir = os.path.dirname(__file__)
or perhaps os.path.dirname(os.path.abspath(__file__)). Try to determine
that early in your program because __file__ may contain a relative path
(and will give a wrong result after changing the current directory).
I do it right at the top of the first module. Thanks for the help.
>
--
Gabriel Genellina

Jul 9 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Eugene Anthony | last post: by
7 posts views Thread by CodeGrommet | 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.