0

In my program, I use the ask filedialog function to allow the user to import some personal pieces of music, but I accept only .wav format. So I use " filetypes=(('".wav"'+" format only","*.wav"),)) " to allow only files with the extension .wav, however it doesn't block the possibility to import internet link present on the computer, which is used in shortcut. How to avoid that the user selects an internet link in this filedialog ?

Thanks for the help !!!!!

(Python 3.8.3, with Tkinter, on Windows 10)

  • why not use os.path.exists(filename) as validation for files? (assuming you use tkFileDialog) – R4PH43L Apr 27 '20 at 17:31
  • Because I understood that this method was to check (with True / False) if a path exists and is present, or in this case the file's name, but I would like in the filedialog to avoid that a user selects a file which can not be used (even if I have already a system in my program to check if the format can be used or not). –  Apr 27 '20 at 22:20
  • I just would like to understand why, even when I specified a specific format, a user can select an URL. It does not make sense, I mean an URL is not a .wav file, so why do my URL appear ? Why can they be selected ??? –  Apr 27 '20 at 22:23
  • What kind of url are we talking? file:///c/temp/x.wav is uri/url and would be fine for that. 127.0.0.1/share/x.wav is one too. File-that-is-a-link-with-extension.wav is also a path that matches. Could you give some detailed examples what exactly you encounter? – R4PH43L Apr 27 '20 at 22:34
  • Ok I see, because on my pc for example I placed a shortcut with the link of this page to have a quick access, so "https://stackoverflow.com/questions/61456040/tkinter-ask-filedialog-avoid-internet-link", which has on the desk for name "hyperlink - Tkinter ask filedialog avoid internet link - Stack Overflow", but I can still select it in the askopenfilename function, despites the fact this URL doesn't contain the extension .wav –  Apr 28 '20 at 04:21
  • In that case - could you please show us the code you used to get to this error? I just tried win10 using python2.7, 3.7 and 3.8 without any success on reproducing the error. Please refer to: https://stackoverflow.com/help/minimal-reproducible-example – R4PH43L Apr 28 '20 at 10:18
  • When I execute this part of my program, corresponding to a function, it's appears : `from tkinter import * from tkinter import filedialog import os root=Tk() list_files=filedialog.askopenfilenames(initialdir=os.path.expanduser("~/Desktop"),filetypes=[("Format .wav only","*.wav")]) print(list_files) root.mainloop() ` –  Apr 28 '20 at 10:53
  • (Sorry I don't manage to implement properly the code) –  Apr 28 '20 at 10:54

1 Answers1

0

I have to correct my previous statement from the comment. That seems to be a flaw in Python 3.X implementations.

I just checked again with python2.7 and used a ".url" file ("Internet Hyperlink") on Windows 10.

".ink"-Files work, ".url"-Files do not.

Python2.7 using

from Tkinter import *
import tkFileDialog as filedialog

Python2.7

Python3.7 using

from tkinter import *
from tkinter import filedialog

Python3.7

R4PH43L
  • 2,122
  • 3
  • 18
  • 30