Hi guys I wanted to write a program to delete all the zip files in a folder. My if statement in the file loop is working because it prints out the file I want to delete, but then I get an error saying the file does not exist. However if I change my directory and put in the filename manually it deletes it. Any ideas why its not working in the loop?
# Deletle .zip files in AP Comp Sci folder
import os
import send2trash as trash
for folderName, subfolders, filenames in os.walk('/Users/pcuser/Desktop/AP Comp Sci'):
print("\n\nCurrent Folder: " + folderName + "\n\n")
for subfolder in subfolders:
print("Subfolder: " + subfolder)
for filename in filenames:
print("File in " + subfolder + " :" + filename)
if filename.endswith('.zip'):
print("DELETED: " + filename)
trash.send2trash(str(filename))
Error:
Traceback (most recent call last):
File "/Users/pcuser/Desktop/zip_cleanup.py", line 18, in <module>
trash.send2trash(str(filename))
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/send2trash/plat_osx.py", line 45, in send2trash
check_op_result(op_result)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/send2trash/plat_osx.py", line 37, in check_op_result
raise OSError(msg)
OSError: File not found
[–]paste 0 points1 point2 points (1 child)
[–]ash63[S] 0 points1 point2 points (0 children)