Reputation: 169
I need to get the selected path within a Filedialog tkinter but I do not know how to because when choosing the path directly in the graphical window save as there is no way to extract the path to store it in a variable
from tkinter import filedialog
open_f = filedialog.asksaveasfile(mode='w', defaultextension='.txt')
path_f =
As you can see I need to store the route obtained in open_f within the variable path_f .. but open_f only returns a _io.textiowrapper..
How do you get the path in a filedialog?
Upvotes: 0
Views: 1526
Reputation: 1
out = filedialog.asksaveasfile(mode='w', defaultextension=".xlsx")
out.close()
restr = str(out)
RegexPrep = restr.replace("'w'", '')
outRegex = re.findall(r"'(.*?)'", RegexPrep)
ToExcelRegex = str(outRegex)
MorePrep = ToExcelRegex.replace("[",'')
MorePrep = MorePrep.replace("]",'')
MorePrep = MorePrep.replace("'",'')
MorePrep = MorePrep.replace(", cp1252",'')
Final = MorePrep.strip()
A simpler way to do it:
open_f = tkFileDialog.asksaveasfile(mode='w', defaultextension='.txt')
path_f = your_file.name
Upvotes: 0
Reputation: 385
Try:
def open():
open_f = filedialog.asksaveasfile(mode='w', defaultextension='.txt')
path_f = open_f.name
# You Can Do Anything Here(e.g. print(path_f))
Full code:
from tkinter import filedialog
def open():
open_f = filedialog.asksaveasfile(mode='w', defaultextension='.txt')
path_f = open_f.name
print(path_f)
# '/home/norok2/.xsession-errors'
# You can run it by typing:
open()
Upvotes: 0
Reputation: 26886
If all you want is actually a filename, use tkinter.filedialog.asksaveasfilename()
:
from tkinter import filedialog
filepath = filedialog.asksaveasfilename()
print(filepath)
# '/home/norok2/.xsession-errors'
Upvotes: 1
Reputation: 5329
You should use the name
method of your object. Like below:
open_f = tkFileDialog.asksaveasfile(mode='w', defaultextension='.txt')
path_f = your_file.name
Upvotes: 0