如何使用tkFileDialog获取文件的绝对路径?

时间:2022-04-27 16:01:04

I am using:

我在用:

file = tkFileDialog.askopenfile(parent=root, mode='rb', 
       filetypes=[('Subrip Subtitle File','*.srt')], title='Choose a subtitle file')

to get a file object specified by the user.

获取用户指定的文件对象。

Is there any way I can get the absolute path of this file from the file object?

有什么办法可以从文件对象中获取此文件的绝对路径吗?

2 个解决方案

#1


12  

file = tkFileDialog.askopenfile(parent=root,mode='rb',filetypes=[('Subrip Subtitle File','*.srt')],title='Choose a subtitle file')
abs_path = os.path.abspath(file.name)

#2


3  

os.path.abspath should do what you want, if I understand your question correctly.

如果我正确理解你的问题,os.path.abspath应该做你想要的。

#1


12  

file = tkFileDialog.askopenfile(parent=root,mode='rb',filetypes=[('Subrip Subtitle File','*.srt')],title='Choose a subtitle file')
abs_path = os.path.abspath(file.name)

#2


3  

os.path.abspath should do what you want, if I understand your question correctly.

如果我正确理解你的问题,os.path.abspath应该做你想要的。