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?
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.
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.
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.
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.
file = tkFileDialog.askopenfile(parent=root,mode='rb',filetypes=[('Subrip Subtitle File','*.srt')],title='Choose a subtitle file')
abs_path = os.path.abspath(file.name)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With