Lucas
Lucas

Reputation: 257

Recover Path in <_io.BufferedReader name='Path'> after browsing a file Tkinter

I'm creating an app with tkinter Python library, and have already this :

class Application(Tk) :

    def __init__(self):
        Tk.__init__(self)
        self.Launch = Button(self, text="Launch", command=self.launchCallBack)
        self.Browse = Button(self, text="Browse", command=self.browseCallBack)
        self.pathlabel = Label(self)

        self.file = ''

        self.Launch.pack()
        self.Browse.pack()
        self.pathlabel.pack()

    def browseCallBack(self) :
        self.file = filedialog.askopenfile(parent=self, mode='rb', title='Choose a file', initialdir = "D:\\Users\T0211254\MyApp\Bundle CUD-CAPELLA 431\melody\eclipse\workspace", filetypes=[("aird Files", "*.aird")])
        self.pathlabel.config(text=str(self.file))

    def launchCallBack(self):
        create_file(self.file)

The problem is that my self.file attribute returns me :

<_io.BufferedReader name='MyFilePath'>

And i just want to recover the MyFilePath.

Thanks for helping !

Upvotes: 1

Views: 1446

Answers (1)

AKX
AKX

Reputation: 169338

The name is available on BufferedReaders in the name attribute, so self.file.name would get you what you want.

However, you probably want to use filedialog.askopenfilename() instead, to just get the name, not an open file object.

Upvotes: 3

Related Questions