首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >send2trash不使用tkinter filedialog

send2trash不使用tkinter filedialog
EN

Stack Overflow用户
提问于 2022-05-07 18:06:42
回答 1查看 92关注 0票数 0

我正在尝试使用send2trash将一个文件发送到垃圾中。使用像send2trash("C:\\path\\to\\file\\file.txt")一样正确,但当使用filedialog.askopenfilename()并将结果输入到它时,没有效果。我尝试过使用str(),但没有效果。

代码:

代码语言:javascript
运行
复制
m = filedialog.askopenfilename(title="Choose which file to shred",filetypes=[("file","*.*")])
send2trash(m) #Also used send2trash(str(m)) but that did not work

错误:

代码语言:javascript
运行
复制
  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'

我不知道为什么这不管用,所以任何帮助都是非常感谢的。

Python 3.10.4,Windows 10

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-05-26 12:51:14

对于python来说非常新,但我认为send2trash只接受路径名中的反斜杠,并且查看使用正斜杠返回路径'askopenfilename‘的错误。

代码语言:javascript
运行
复制
m = filedialog.askopenfilename(title="Choose which file to shred",filetypes=[("file","*.*")])
m = m.replace('/', '\\')
send2trash(m)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72155221

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档