我是 bukkit 插件编码器,我一直在制作配置文件。我想用来__file__
定位 .py 文件的当前目录...我在谷歌上搜索并看到了sys.argv[0]
,但正如我所见,它仅适用于 Windows 用户,我希望它与每个操作系统兼容,如果我错了,你能给我一个例子吗?
这是我的代码:
def onEnable(self):
pluginlokacija1 = os.path.realpath(__file__)
if os.path.isfile("%s/Halloween/config.yml"%pluginlokacija1):
pass
else:
os.mkdir("%s/Halloween/"%pluginlokacija1)
self.createConfig()
self.findFolder()
self.cfg = YamlConfiguration.loadConfiguration(File(os.path.join(self.folder,"config.yml")))
itemlist = self.cfg.getString("main.itemlist")
print ("Rezultat: %s" % itemlist)
def createConfig(self):
pluginlokacija2 = os.path.realpath(__file__)
fo = open("%s/Halloween/config.yml"%pluginlokacija2, "wb")
fo.write( "main:\n itemlist: 172\n candydropchance: 50");
fo.close
错误:
14:54:06 [SEVERE] Error occurred while enabling Halloween v1.0 (Is it up to date
?): null
Traceback (most recent call last):
File "<iostream>", line 70, in onEnable
NameError: global name '__file__' is not defined
谢谢回答!:)