Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the absolute path of a file using tkFileDialog?

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?

like image 790
abcde123483 Avatar asked Dec 10 '09 14:12

abcde123483


People also ask

How to show file path in Tkinter?

To display the filedialog that starts from a particular location, use the initialdir = <location> argument in the static factory function askopenfilename(initialdir=<location>). This function creates a modal-like dialogbox and waits for the user's selection and returns the value of the selected file to the caller.

What is filedialog in Tkinter?

filedialog — File selection dialogs. Source code: Lib/tkinter/filedialog.py. The tkinter. filedialog module provides classes and factory functions for creating file/directory selection windows.

What is absolute file path in Python?

An absolute file path describes how to access a given file or directory, starting from the root of the file system. A file path is also called a pathname. Relative file paths are notated by a lack of a leading forward slash. For example, example_directory.

What is askopenfilename in Tkinter?

Introduction to the Tkinter Open File Dialog functions The askopenfilename() function returns the file name that you selected. The askopenfilename() also supports other useful options including the initial directory displayed by the dialog or filtering files by their extensions.


1 Answers

file = tkFileDialog.askopenfile(parent=root,mode='rb',filetypes=[('Subrip Subtitle File','*.srt')],title='Choose a subtitle file')
abs_path = os.path.abspath(file.name)
like image 96
Dan Lorenc Avatar answered Oct 21 '22 05:10

Dan Lorenc