So, I am making a python project that deletes files in a directory and my code is:
tmp_dir = r"C:\Users\User\AppData\Roaming\Microsoft\Teams\tmp"
tmp_list = os.listdir(tmp_dir)
for tmp_files in tmp_list:
shutil.rmtree(os.path.join(tmp_dir, tmp_files))
and I am getting the following error:
Traceback (most recent call last): File "<my program's location>", line 9, in shutil.rmtree(os.path.join(tmp_dir, tmp_files)) File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\shutil.py", line 737, in rmtree return _rmtree_unsafe(path, onerror) File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\shutil.py", line 596, in _rmtree_unsafe onerror(os.scandir, path, sys.exc_info()) File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\shutil.py", line 593, in _rmtree_unsafe with os.scandir(path) as scandir_it: NotADirectoryError: [WinError 267] The directory name is invalid: 'C:\Users\User\AppData\Roaming\Microsoft\Teams\tmp\x64.json'
Process finished with exit code 1
Can you please tell me what is wrong in my code? Any help is appreciated.
shutil.rmtree
deletes an entire directory tree.
To remove a single file, you can use os.remove
.