如何使用python中的图像名称将图像拆分为两个文件夹?

如何使用python中的图像名称将图像拆分为两个文件夹?,python,Python,我有一个包含 ['PAT_01_01_crypts.png', 'PAT_01_01_orig.png', 'PAT_01_02_crypts.png', 'PAT_01_02_orig.png', 'PAT_01_03_crypts.png', 'PAT_01_03_orig.png'] 我想把这些图像分成两个文件夹 文件夹1: PAT_01_01_crypts.png PAT_01_02_crypts.png PAT_01_03_crypts.png 文件夹2: PAT_0

我有一个包含

['PAT_01_01_crypts.png',

'PAT_01_01_orig.png',

'PAT_01_02_crypts.png',

'PAT_01_02_orig.png',

'PAT_01_03_crypts.png',

'PAT_01_03_orig.png']
我想把这些图像分成两个文件夹

文件夹1:

PAT_01_01_crypts.png

PAT_01_02_crypts.png

PAT_01_03_crypts.png
文件夹2:

PAT_01_01_orig.png

PAT_01_02_orig.png

PAT_01_03_orig.png

你能帮帮我吗。我也在尝试增加这些图像。

您可以在关键字中使用
,检查语句中的特定字符串是否正确

A = ['PAT_01_01_crypts.png', 'PAT_01_01_orig.png', 'PAT_01_02_crypts.png',
     'PAT_01_02_orig.png', 'PAT_01_03_crypts.png', 'PAT_01_03_orig.png']


folder1 = []
folder2 = []

for name in A:
    if "orig" in name:
        folder1.append(name)
    else:
        folder2.append(name)

print(folder1)
print(folder2)
或者您可以使用:

folder1 = [name for name in A if "orig" in name]
folder2 = [name for name in A if "crypt" in name]
两种输出:

['PAT_01_01_orig.png', 'PAT_01_02_orig.png', 'PAT_01_03_orig.png']
['PAT_01_01_crypts.png', 'PAT_01_02_crypts.png', 'PAT_01_03_crypts.png']

您可以将
shutil
库与
glob
库结合使用



import shutil
import os
import glob


path = os.path.join('src','data','images','*')
#use your own path
list_files = glob.glob(path)
for file in list_files:
  if file.endswith('orig.png'):
     shutil.move(file,os.path.join('src','data','target','')
  else:
     shutil.move(file,os.path.join('src','data','target2','')

     
glob.glob(path)
将返回一个包含路径中所有文件/文件夹的列表。请注意,要执行此操作,您需要路径末尾的
*

shutil.move()
有关详细信息,请访问


我还没有测试过它,所以请告诉我它是否有效

这里有一个解决这个问题的新手方法。该方法很简单,但可以考虑任意数量的
图像类型
(orig、crypt等)

代码 输入 输出
感谢您的快速回复!谢谢你的快速回复!!很乐意帮忙。如果这个或任何其他答案对你有帮助,请接受并投票。stackoverflow.com/help/someone-answers
import os 
import shutil 

image_type = ['orig', 'crypt', 'hd', 'sd']
files_list = ['PAT_01_01_crypts.png', 'PAT_01_02_crypts.png', 'PAT_01_03_crypts.png', 'PAT_01_01_orig.png', 'PAT_01_02_orig.png', 'PAT_01_03_orig.png', 'myfile_hd.png', 'yourfile_sd.png']

# Check if directory exists. If not, create it
for folder in image_type:
    if os.path.isdir(f"./{folder}"):
        pass
    else:
        os.mkdir(f"./{folder}")

# Move files
for fname in files_list:  # traverse the files list
    for img in image_type: # compare which type it matches to
        if img in fname:
            shutil.move(f"./{fname}", f"./{img}/{fname}") # move it to the respective directory
            break 

# Print contents of each folder
for folder in image_type:
    print(f"\n\nContents of {folder}::::")
    print(os.listdir(f"./{folder}"))
PAT_01_01_crypts.png
PAT_01_02_crypts.png
PAT_01_03_crypts.png
PAT_01_01_orig.png
PAT_01_02_orig.png
PAT_01_03_orig.png
myfile_hd.png
yourfile_sd.png
Contents of orig::::
['PAT_01_01_orig.png', 'PAT_01_02_orig.png', 'PAT_01_03_orig.png']


Contents of crypt::::
['PAT_01_01_crypts.png', 'PAT_01_02_crypts.png', 'PAT_01_03_crypts.png']


Contents of hd::::
['myfile_hd.png']


Contents of sd::::
['yourfile_sd.png']