Reputation: 31
I am trying to delete folders and files within this path: 'Users/ryanunderwood/Desktop'
Here is my code:
import shutil
shutil.rmtree('Users/ryanunderwood/Desktop')
However, this error is being thrown:
Traceback (most recent call last):
File "/Users/ryanunderwood/Documents/Python files/wipeComputer.py", line 10, in <module>
shutil.rmtree('Users/ryanunderwood/Desktop')
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/shutil.py", line 516, in rmtree
return _rmtree_unsafe(path, onerror)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/shutil.py", line 377, in _rmtree_unsafe
onerror(os.scandir, path, sys.exc_info())
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/shutil.py", line 374, in _rmtree_unsafe
with os.scandir(path) as scandir_it:
FileNotFoundError: [Errno 2] No such file or directory: 'Users/ryanunderwood/Desktop'
How do I fix this?
Upvotes: 1
Views: 555
Reputation: 436
There are two kinds of paths: local and absolute paths.
A local path (like you're currently using) is a list of of folders, and will be interpreted as starting in the current working folder. Because of that, the result will depend on where/how you're running you're programm (the working dictionary).
If the folder where you're running python contained a folder 'Foo', shutil.rmtree('Foo')
would actually work.
However, that's probably not what you want here. If you always want to target the same folder, use an absolute path, which starts with the drive specification ('C:'
in this case) and contains the whole list of nested folders until you reach your target.
In this case "C:/Users/ryanunderwood/Desktop"
, assuming you have the standard folder structure of Windows.
Upvotes: 1
Reputation: 2744
You are not specifying the whole Path to your desktop directory correctly. Probably it's something like shutil.rmtree("C:/Users/ryanunderwood/Desktop")
depending on on what drive/partition your Desktop folder resides.
Upvotes: 1