I have mucked things up when trying to make a script to copy and rename files. I have ended up copying files into the same directory that the file existed. I wanted to copy and rename the files and add to a new directory. I now need to delete files which contain a certain string located in various places within the filename. I am able to print these files with the below code, but unable to delete them. Any help would be much appreciated!
import os
import shutil as sh
from pathlib import Path
from pathlib import PurePath
root_path = PurePath("/Users/codychandler/SyncDrive/Colocation Information(Kory.Williams@charter.com )")
#dest_path = PurePath(r"/Users/codychandler/Docs/MSA_SO Crawl2")
exts = [".pdf", ".doc", ".docx"]
str = "Kory.Williams"
for root, _, fnames in os.walk(root_path):
for f in fnames:
if f.endswith(tuple(exts)) and str in f:
f = os.path.abspath(f)
print (f)
os.remove(os.path.join(root,f))
EDIT:
This code returns the following:
FileNotFoundError: [Errno 2] No such file or directory: