我有一个包含 10 个图像的文件夹,我希望根据它的当前文件名移动到一个新文件夹中。我已经成功地将文件夹中的每个图像移动到一个新文件夹中,到目前为止我已经成功地将每个图像文件名移动到了自己的文件夹中,但我还没有弄清楚如何移动所有图像相同的文件名放入一个文件夹,另一个放入另一个文件夹。例如下面我想相应地移动图像。
到目前为止,这是我通过基于文件名创建新文件夹将图像文件移动到新文件夹的代码。我得到了制作文件夹的部分,但是当它为所有图像创建单独的图像文件夹时我很困惑。
import os, shutil, glob
#Source file
sourcefile = 'Desktop/00/'
# for loop then I split the names of the image then making new folder
for file_path in glob.glob(os.path.join(sourcefile, '*.jpg*')):
new_dir = file_path.rsplit('.', 1)[0]
# If folder does not exist try making new one
try:
os.mkdir(os.path.join(sourcefile, new_dir))
# except error then pass
except …Run Code Online (Sandbox Code Playgroud) python image batch-processing python-imaging-library python-3.x