How to get the pure file path from tkinter's filedialog.askopenfile()?

In the code sourceDir = filedialog.askopenfile() that was posted in the original question, the filename can be accessed via sourceDir.name.

Alternatively, the tkinter.filedialog module has an askopenfilename function. This function will only return the filename string.

sourceDir = filedialog.askopenfilename()
print(f"source file: {sourceDir}")

you can use sourceDir.name

which will give you the "D:/GitHub/repo/test.jpg"

sourceDir = filedialog.askopenfile() print(f"source file: {sourceDir.name}")