我有一个搜索和打开文件的代码:
def OpenButton(self, event):
filedialog = wx.FileDialog(self, message = 'Open text file',
defaultDir = '.',
defaultFile = 'TestTOC.txt',
wildcard = "Text source (*.txt)|*.txt|" "All files (*.*)|*.*",
style = wx.OPEN)
if filedialog.ShowModal() == wx.ID_OK:
print filedialog.GetPath()
event.Skip()
它会显示文件的路径:C:\....\Desktop\test.txt
我有另一个代码需要读取我选择的文件:
def ReadButton(self, event):
file=open('C:....\Desktop\test.txt','r') # the same path as above
text=file.read()
file.close()
如何复制该路径并将其替换为open(....,'r')?
答案 0 :(得分:4)
使用变量?
def OpenButton(self, event):
filedialog = wx.FileDialog(self, message = 'Open text file',
defaultDir = '.',
defaultFile = 'TestTOC.txt',
wildcard = "Text source (*.txt)|*.txt|" "All files (*.*)|*.*",
style = wx.OPEN)
if filedialog.ShowModal() == wx.ID_OK:
self.filepath = filedialog.GetPath()
event.Skip()
def ReadButton(self, event):
file=open(self.filepath,'r') # the same path as above
text=file.read()
file.close()
答案 1 :(得分:2)
更改
print filedialog.GetPath()
到
path = filedialog.GetPath()
print path
然后用路径变量做任何你想做的事。