0

ファイルを検索して開くためのコードがあります:

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')に置き換えるにはどうすればよいですか?

4

2 に答える 2

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()  
于 2012-05-31T17:26:23.960 に答える
2

変化する

print filedialog.GetPath()

path = filedialog.GetPath()
print path

次に、パス変数で必要なことを行います。

于 2012-05-31T17:26:30.867 に答える