I'm trying to move file to recycle bin using shutil library. Following is relevant code lines, but I get kinda strange error. Both files are local, and I can access both locations on my PC. Why this error occurs? Because I run Main.py it from F:?
import shutil
# Path to folder where files should be trashed
dump_folder = r"C:\$Recycle.Bin\\"
file_name = "C:\\Storage\\\\statuti.docx"
# Move it to storage directory
shutil.move(file_name, dump_folder)
Error message
F:\Python\Project\venv\Scripts\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2020.1\plugins\python-ce\helpers\pydev\pydevd.py" --multiproc --qt-support=auto --client 127.0.0.1 --port 54648 --file F:\Python\Main.py Connected to pydev debugger (build 203.5981.165) Traceback (most recent call last): File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\shutil.py", line 788, in move os.rename(src, real_dst) PermissionError: [WinError 5] Access is denied: 'C:\Storage\\statuti.docx' -> 'C:\Recycle.Bin\\'
During handling of the above exception, another exception occurred:
Traceback (most recent call last): File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\shutil.py", line 261, in copyfile with open(src, 'rb') as fsrc, open(dst, 'wb') as fdst: OSError: [Errno 22] Invalid argument: 'C:\Recycle.Bin\\'
To send something to the recycle bin it is much easier to use send2trash
. It is cross platform and very easy to use.
You can install it with :
pip install Send2Trash
Then you can use it :
from send2trash import send2trash
send2trash(filename)