I have two folders that are supposed to have the same filenames except one folder is all JPG and the other is JSON. There are many extra JPGs.
I know I have to do something like val.split('.') for val in os.listdir() and os.remove() or something, but can't figure out the syntax.
I want to iterate over both folders, and if there are any extra JPGs that don't have a corresponding JSON, I want to delete that JPG.
Probably not the most elegant solution, but you can implement something like:
#DirA files have .jpg extension
dir_a = []
#DirB files have .json extension
dir_b = []
for fileA in os.listdir(path_to_dir_a):
dir_a.append[fileA.split('.')[0]]
for fileB in os.listdir(path_to_dir_a):
dir_b.append[fileB.split('.')[0]]
for fileA in dir_a:
if not fileA in dir_b:
os.remove(os.path.join(path_to_dir_a,(fileA+'.jpg')))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.