擅长:python、mysql、java
<p>根据<a href="http://www.cocoadev.com/index.pl?MoveToTrash" rel="nofollow noreferrer">http://www.cocoadev.com/index.pl?MoveToTrash</a>中的代码,我得出以下结论:</p>
<pre><code>def get_trash_path(input_file):
path, file = os.path.split(input_file)
if path.startswith("/Volumes/"):
# /Volumes/driveName/.Trashes/<uid>
s = path.split(os.path.sep)
# s[2] is drive name ([0] is empty, [1] is Volumes)
trash_path = os.path.join("/Volumes", s[2], ".Trashes", str(os.getuid()))
if not os.path.isdir(trash_path):
raise IOError("Volume appears to be a network drive (%s could not be found)" % (trash_path))
else:
trash_path = os.path.join(os.getenv("HOME"), ".Trash")
return trash_path
</code></pre>
<p>相当基本,有一些事情必须分开做,特别是检查文件名是否已经存在于垃圾箱中(以避免覆盖)和实际移动到垃圾箱,但它似乎涵盖了大多数事情(内部、外部和网络驱动器)</p>
<p><strong>更新:</strong>我想在Python脚本中丢弃一个文件,所以我在Python中重新实现了Dave Dribin的解决方案:</p>
^{pr2}$
<p>用法很简单:</p>
<pre><code>trashPath("/tmp/examplefile")
</code></pre>