Selecting a file in a directory

V

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?
 
A

Alf P. Steinbach

* 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
 
V

vsoler

* vsoler:





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
 
S

Steven D'Aprano

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?


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

or google on "python display file dialog".
 
R

rantingrick

* vsoler:





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()
 
V

vsoler

* vsoler:
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!
 
R

rantingrick

(..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 --#
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top