[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Selecting a file in a directory

vsoler

2/14/2010 1:22:00 AM

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?
6 Answers

Alf P. Steinbach

2/14/2010 1:28:00 AM

0

* 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

vsoler

2/14/2010 1:39:00 AM

0

On Feb 14, 2:28 am, "Alf P. Steinbach" <al...@start.no> 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

Steven D'Aprano

2/14/2010 1:45:00 AM

0

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/tk...
http://wiki.python.org/moin/GuiP...

or google on "python display file dialog".


--
Steven

rantingrick

2/14/2010 1:46:00 AM

0

On Feb 13, 7:28 pm, "Alf P. Steinbach" <al...@start.no> 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()

vsoler

2/14/2010 2:00:00 AM

0

On Feb 14, 2:45 am, rantingrick <rantingr...@gmail.com> wrote:
> On Feb 13, 7:28 pm, "Alf P. Steinbach" <al...@start.no> 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!

rantingrick

2/14/2010 2:43:00 AM

0

On Feb 13, 8:00 pm, vsoler <vicente.so...@gmail.com> wrote:
> On Feb 14, 2:45 am, rantingrick <rantingr...@gmail.com> 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 --#