gpt4 book ai didi

python - 使用 Mac OS 将文件从垃圾箱移动到 python 中的文件夹

转载 作者:太空宇宙 更新时间:2023-11-03 14:23:22 26 4
gpt4 key购买 nike

trash = "~/.Trash"
new_folder = []
for current_file in os.listdir(trash):
new_folder.append(current_file)

上面的代码不起作用。我正在尝试将文件从垃圾箱移动到 Mac 上的 python 文件夹中。我收到的错误消息是

FileNotFoundError: [Errno 2] No such file or directory: '~/.Trash'

最佳答案

扩展 ~ 是 shell 的一项功能。您不能像普通文件系统路径一样使用它。幸运的是,Python 有一个函数可以帮助您做到这一点:

trash = os.path.expanduser('~/.Trash')

参见https://docs.python.org/3/library/os.path.html#os.path.expanduser了解详情。

关于python - 使用 Mac OS 将文件从垃圾箱移动到 python 中的文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47797911/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com