Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

How to use File Open dialog to get file path in Python

📅 2014-Feb-25 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ python ⬩ 📚 Archive

It is pretty easy to pass a filename as a command-line argument to a Python program. However, this can be cumbersome if the users of your program are naive or you need to pass a path that is long or difficult. In such cases, a good solution is to display a File Open dialog and let the user pick the file she wants.

This can be done easily using the Tk library that ships with Python:

import Tkinter
import tkFileDialog

def main():

    Tkinter.Tk().withdraw() # Close the root window
    in_path = tkFileDialog.askopenfilename()
    print in_path

if __name__ == "__main__":

Tried with: Python 2.7.3 and Ubuntu 12.04 LTS

© 2022 Ashwin Nanjappa • All writing under CC BY-SA license • 🐘📧