【发布时间】:2015-05-08 18:04:53
【问题描述】:
我一直致力于创建自己的 .py 文件来存储该方法的处理程序的项目,我一直在尝试弄清楚如何将 Python 文件存储在文件夹中并打开它们。这是我用来创建文件的代码,如果它们不存在,然后导入文件:
if os.path.isfile("Btn"+ str(self.ButtonSet[self.IntBtnID].IntPID) +".py") == False:
TestPy = open("Btn"+ str(self.ButtonSet[self.IntBtnID].IntPID) +".py","w+")
try:
TestPy.write(StrHandler)
except Exception as Error:
print(Error)
TestPy.close()
self.ButtonSet[self.IntBtnID].ImpHandler = __import__("Btn" + str(self.IntBtnID))
self.IntBtnID += 1
当我改变这一行时:
self.ButtonSet[self.IntBtnID].ImpHandler = __import__("Btn" + str(self.IntBtnID))
到这里:
self.ButtonSet[self.IntBtnID].ImpHandler = __import__("Buttons\\Btn" + str(self.IntBtnID))
无法找到填充并最终引发错误,因为它在文件夹中找不到文件。
知道为什么它不起作用我只是不知道如何解决这个问题:/
我的问题是 .py 存储在文件夹中时如何打开它?
【问题讨论】:
标签: python file python-import relative-path