Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
369 views
in Technique[技术] by (71.8m points)

directory - python : Move same images inside same folder progamtically

Below code successfully creates all folder with same as image names. Now, I want to move images with same name inside same folder. This code creates folders but images were not moved inside folders.

import os
import shutil
images = os.listdir(os.getcwd())
print(images)
ids = set(i.split('_')[0] for i in images)  # set removes duplicates
for i in ids:
    os.mkdir(i)  # create subdirs
for img in images:
    target_dir = img.split('_')[0]
    shutil.move(img, target_dir)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Correct one. Just remove .py file

import os
import shutil

images = os.listdir(os.getcwd())

bad_files = set(["morph.py"])

ids = set(i.split('_')[0] for i in images)  # set removes duplicates
ids.remove("morph.py")
print(ids)
for i in ids:
    os.mkdir(i)  # create subdirs
for img in images:
    target_dir = img.split('_')[0]
    shutil.move(img, target_dir)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...