开发者

Is there a way to tell where your python file is?

(Python2.7-3.2 on WindowsXP) I used to use sys.arg[0], but now I'm开发者_JAVA技巧 executing it from a batch script(for determining if the user has python)

Thanks ^^

~ps: Suggest if you know a better way to find out if the user has Python


Maybe you are looking for

os.path.dirname(__file__)


You can read the variable __file__ to find out the path to the current Python file being interpreted.


you could take a look at this posting that checks the registry

batch file to check if Python is installed

seems to be easy as:

reg query "hkcu\software\Python 2.6"

An alternative is to check the %PATH%. I am not sure how to do that exactly, as do not have a installed windows around to test it:)

Try to find out more about "reg query" and how to search %PATH% for executables!

Good luck!!


To add to this, although it is already answered, here is something I got from this site (don't remember the source)

import os, sys
print "CWD: ", os.getcwd()
print "Script: ", sys.argv[0]
print ".EXE: ", os.path.dirname(sys.executable)
print "Script dir: ", os.path.realpath(os.path.dirname(sys.argv[0]))
pathname, scriptname = os.path.split(sys.argv[0])
print "Relative script dir: ", pathname
print "Script dir: ", os.path.abspath(pathname)
print 'Current file: ', os.path.dirname(__file__)
print os.path.dirname(__file__) + '/IDLESTARTUP.txt'


os.path.normpath(os.path.abspath(os.path.dirname(__file__)))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜