'send2trash not working with tkinter filedialog
I am trying to send a file to the trash using send2trash. Used like send2trash("C:\\path\\to\\file\\file.txt") works correctly but when using filedialog.askopenfilename() and feeding the results in to it doesn't. I have tried using str() to no avail.
Code:
m = filedialog.askopenfilename(title="Choose which file to shred",filetypes=[("file","*.*")])
send2trash(m) #Also used send2trash(str(m)) but that did not work
Error:
File "c:\Python310\Scripts\delete.py", line 293, in <module>
send2trash(str(m))
File "C:\Python310\lib\site-packages\send2trash\plat_win_legacy.py", line 115, in send2trash
File "C:\Python310\lib\site-packages\send2trash\plat_win_legacy.py", line 115, in <listcomp>
paths = [get_short_path_name(path) for path in paths]
File "C:\Python310\lib\site-packages\send2trash\plat_win_legacy.py", line 99, in get_short_path_name
raise WindowsError(err_no, FormatError(err_no), long_path)
FileNotFoundError: [Errno 2] The system cannot find the file specified.: '\\\\?\\C:/Python310/Scripts/testfile.txt'
I don't know why this isn't working so any help is very much appreciated.
Python 3.10.4, Windows 10
Solution 1:[1]
Very new to python but I think send2trash only accepts backslashes in pathname and looking at the error 'askopenfilename' is returning path using forward slashes
m = filedialog.askopenfilename(title="Choose which file to shred",filetypes=[("file","*.*")])
m = m.replace('/', '\\')
send2trash(m)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Graham Ince |
