简体   繁体   中英

How can I find the currently executing script file and path in python?

I am trying to get the file name and directory of the currently executing script and this is the code that works:

import inspect, os
print os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))

In php this is just echo dirname(__FILE__) . Nice and simple.

There are suggestions that dirname(__file__) should work in Python but it returns an empty string when running via the command line.

My question is that why do I have to write all of this code to get the filename:

print os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))

and am I doing it wrong?

The __file__ value for the current script is the exact same path you passed to Python; python yourscript.py is thus a relative path and doesn't include the directory name. You do need to make it an absolute path, but you do not need to use inspect :

import os.path

print os.path.dirname(os.path.abspath(__file__))

Demo:

$ cat /private/tmp/test.py 
import os.path

print os.path.dirname(os.path.abspath(__file__))

$ python /private/tmp/test.py 
/private/tmp
$ cd /private/tmp/
$ python test.py 
/private/tmp

In PHP, you have the luxury of the web server executing your files with fully specified absolute paths, so you never saw this 'problem' crop up there.

My question is that why do i have to write all of this code to get the filename:

print os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))

Let's take it piece by piece.

First, the print is 1 character longer than PHP's echo . I doubt you have a problem with that.

Next, you have to use os.path.dirname instead of just dirname because Python allows code to be organized into modules, and allows you to reference things by qualified name. This is why Python can have functions like gzip.open and io.open that have the same name. But it doesn't force you to use qualified names if you don't want to; if you'd done, say, from os.path import dirname instead of import os , you could just use dirname .

Next, you need the abspath because you may have a relative path. This is actually exactly the same as in PHP. It's just that you're used to running PHP from a web browser setup that always runs your scripts by absolute path. You can do the same in Python if you want.

Finally:

There are suggestions that dirname(__file__) should work in python but it returns an empty string when running via the command line.

Not true.

$ cat > test.py
print __file___
$ python test.py
test.py

If by "running via the command line" you meant "using the interactive interpreter", of course, there is no __file__ , because there is no file (except the terminal stdin).

Or, if you're printing the dirname of a relative path like test.py , of course that's always going to be an empty string, just as it would be in PHP.

Putting it all together:

$ cat > test.py
from os.path import dirname
print dirname(__file__)
$ python $(abspath test.py)
/Users/abarnert/src/test

Just like PHP.

In case __file__ is not set, you can get the directory via

import sys, os
script = os.path.abspath(sys.argv[0] or 'unknown script')
directory = os.path.dirname(script))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM