Selecting a file in a directory

Discussion in 'Python' started by vsoler, Feb 14, 2010.

  1. vsoler

    vsoler Guest

    Hi,

    My python script needs to work with a .txt file in a directory. I
    would like to give the user the possibility to choose the file he
    needs to work on in as much the same way as I open a .xls file in
    Excel, that is, I want to make appear the "Windows'" window and let
    the user choose.

    I think this should be quite straightforward.

    How should I proceed?
    vsoler, Feb 14, 2010
    #1
    1. Advertising

  2. * vsoler:
    > Hi,
    >
    > My python script needs to work with a .txt file in a directory. I
    > would like to give the user the possibility to choose the file he
    > needs to work on in as much the same way as I open a .xls file in
    > Excel, that is, I want to make appear the "Windows'" window and let
    > the user choose.
    >
    > I think this should be quite straightforward.
    >
    > How should I proceed?


    At least in Windows one easy way is to delegate that responsibility to the
    Windows shell. When a user drags a file onto your script, your script is run
    with the path to that dragged file as argument. Or it can even be multiple files.

    Otherwise, tkinter has, as I recall, a standard file chooser dialog.

    These "standard" dialogs are generally called "common dialogs".

    Just google for tkinter and suitable words.



    Cheers & hth.,

    - Alf
    Alf P. Steinbach, Feb 14, 2010
    #2
    1. Advertising

  3. vsoler

    vsoler Guest

    On Feb 14, 2:28 am, "Alf P. Steinbach" <> wrote:
    > * vsoler:
    >
    > > Hi,

    >
    > > My python script needs to work with a .txt file in a directory. I
    > > would like to give the user the possibility to choose the file he
    > > needs to work on in as much the same way as I open a .xls file in
    > > Excel, that is, I want to make appear the "Windows'" window and let
    > > the user choose.

    >
    > > I think this should be quite straightforward.

    >
    > > How should I proceed?

    >
    > At least in Windows one easy way is to delegate that responsibility to the
    > Windows shell. When a user drags a file onto your script, your script is run
    > with the path to that dragged file as argument. Or it can even be multiple files.
    >
    > Otherwise, tkinter has, as I recall, a standard file chooser dialog.
    >
    > These "standard" dialogs are generally called "common dialogs".
    >
    > Just google for tkinter and suitable words.
    >
    > Cheers & hth.,
    >
    > - Alf


    I'll try, thank you very much
    vsoler, Feb 14, 2010
    #3
  4. On Sat, 13 Feb 2010 17:21:51 -0800, vsoler wrote:

    > Hi,
    >
    > My python script needs to work with a .txt file in a directory. I would
    > like to give the user the possibility to choose the file he needs to
    > work on in as much the same way as I open a .xls file in Excel, that is,
    > I want to make appear the "Windows'" window and let the user choose.
    >
    > I think this should be quite straightforward.


    Python is multi-platform. Having the Windows file selection dialogs
    appear under Linux or Mac is anything but straightforward! The only
    general purpose (non-operating system specific) solution will require a
    GUI toolkit.


    > How should I proceed?



    >>> import tkFileDialog
    >>> help(tkFileDialog)



    See also:
    http://docs.python.org/library/tkinter.html
    http://wiki.python.org/moin/GuiProgramming

    or google on "python display file dialog".


    --
    Steven
    Steven D'Aprano, Feb 14, 2010
    #4
  5. vsoler

    rantingrick Guest

    On Feb 13, 7:28 pm, "Alf P. Steinbach" <> wrote:
    > * vsoler:
    >
    > > Hi,

    >
    > > My python script needs to work with a .txt file in a directory. I
    > > would like to give the user the possibility to choose the file he
    > > needs to work on in as much the same way as I open a .xls file in
    > > Excel, that is, I want to make appear the "Windows'" window and let
    > > the user choose.

    >
    > > I think this should be quite straightforward.

    >
    > > How should I proceed?

    >
    > At least in Windows one easy way is to delegate that responsibility to the
    > Windows shell. When a user drags a file onto your script, your script is run
    > with the path to that dragged file as argument. Or it can even be multiple files.
    >
    > Otherwise, tkinter has, as I recall, a standard file chooser dialog.
    >
    > These "standard" dialogs are generally called "common dialogs".


    specifically Alf was referring to tkFileDialog.askopenfilename().
    Heres an example...


    import Tkinter as tk
    from tkFileDialog import askopenfilename

    root = tk.Tk()

    def get_files():
    path = askopenfilename(filetypes=[('TXT', '.txt')])
    if path:
    print path

    tk.Button(root, text='1', font=('Wingdings', 12),
    command=get_files).pack(padx=5, pady=5)

    root.mainloop()
    rantingrick, Feb 14, 2010
    #5
  6. vsoler

    vsoler Guest

    On Feb 14, 2:45 am, rantingrick <> wrote:
    > On Feb 13, 7:28 pm, "Alf P. Steinbach" <> wrote:
    >
    >
    >
    > > * vsoler:

    >
    > > > Hi,

    >
    > > > My python script needs to work with a .txt file in a directory. I
    > > > would like to give the user the possibility to choose the file he
    > > > needs to work on in as much the same way as I open a .xls file in
    > > > Excel, that is, I want to make appear the "Windows'" window and let
    > > > the user choose.

    >
    > > > I think this should be quite straightforward.

    >
    > > > How should I proceed?

    >
    > > At least in Windows one easy way is to delegate that responsibility to the
    > > Windows shell. When a user drags a file onto your script, your script is run
    > > with the path to that dragged file as argument. Or it can even be multiple files.

    >
    > > Otherwise, tkinter has, as I recall, a standard file chooser dialog.

    >
    > > These "standard" dialogs are generally called "common dialogs".

    >
    > specifically Alf was referring to tkFileDialog.askopenfilename().
    > Heres an example...
    >
    > import Tkinter as tk
    > from tkFileDialog import askopenfilename
    >
    > root = tk.Tk()
    >
    > def get_files():
    >     path = askopenfilename(filetypes=[('TXT', '.txt')])
    >     if path:
    >         print path
    >
    > tk.Button(root, text='1', font=('Wingdings', 12),
    > command=get_files).pack(padx=5, pady=5)
    >
    > root.mainloop()


    Excellent!!! Just what I needed!
    vsoler, Feb 14, 2010
    #6
  7. vsoler

    rantingrick Guest

    On Feb 13, 8:00 pm, vsoler <> wrote:
    > On Feb 14, 2:45 am, rantingrick <> wrote:


    (..snip..)

    > Excellent!!! Just what I needed!


    For your case, since it seems you are writing a "console type"
    application you may want to subdue the root window and show the user a
    file dialog window *only*. You can do this by using "root.withdraw()"
    to hide the root window. Just make sure to destroy the root window
    after the users is finished choosing their file (or at some
    appropriate time later) or else your program will not exit gracefully
    at close time...!

    Heres a way to wrap the whole enchilada into a reusable function, of
    course many refinements could
    be made but this is a simplistic version...

    #-- start script --#
    # works on python < 3.0
    import Tkinter as tk
    from tkFileDialog import askopenfilename

    def showFileDialog():
    root = tk.Tk()
    root.withdraw()
    path = askopenfilename(filetypes=[('TXT', '.txt')])
    if path:
    print path
    # do something useful here...
    root.destroy()
    root.mainloop()


    showFileDialog()
    raw_input('press enter to quit...')
    #-- end script --#
    rantingrick, Feb 14, 2010
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave56

    Selecting a file client side

    Dave56, Aug 2, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    349
    Kevin Spencer
    Aug 2, 2004
  2. http://www.visual-basic-data-mining.net/forum

    Displaying image after selecting an image file.

    http://www.visual-basic-data-mining.net/forum, Sep 15, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    341
    http://www.visual-basic-data-mining.net/forum
    Sep 15, 2004
  3. ~~~ .NET Ed ~~~
    Replies:
    0
    Views:
    307
    ~~~ .NET Ed ~~~
    Mar 7, 2005
  4. Nenefta

    Selecting a file on an Editpage

    Nenefta, Apr 19, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    303
    Nenefta
    Apr 20, 2006
  5. Kilicaslan Fatih
    Replies:
    1
    Views:
    253
    Diez B. Roggisch
    Jul 6, 2006
Loading...

Share This Page