#------------------------------------------------------------------------------ # got python script self file name # extract out xxx from: # D:\yyy\zzz\xxx.py # xxx.py def extractFilename(inputStr) : argv0List = inputStr.split("\\"); scriptName = argv0List[len(argv0List) - 1]; # get script file name self possibleSuf = scriptName[-3:]; if possibleSuf == ".py" : scriptName = scriptName[0:-3]; # remove ".py" return scriptName;
例 2.5. extractFilename的使用范例
if __name__=="__main__": # for : python xxx.py -s yyy # -> sys.argv[0]=xxx.py # for : xxx.py -s yyy # -> sys.argv[0]=D:\yyy\zzz\xxx.py scriptSelfName = extractFilename(sys.argv[0]);