遍历提取文件夹中特定的jpg图片并存入指定文件夹

# -*- coding: utf-8 -*-
#!/usr/bin/python
#test_copyfile.py

import os,shutil

rootdir='/home/unbuntu/Desktop/yixian'     #要提取文件夹的根目录
dstdir0='/home/unbuntu/Desktop/胰腺数据()/YIXIAN/yuantu'   #存放原图的文件夹
dstdir1='/home/unbuntu/Desktop/胰腺数据()/YIXIAN/biaoji'   #存放标记图像的文件夹

#def filecopy(path):

for root,dirs,files in os.walk(rootdir):  #遍历根目录下的文件
    for name in files:
        newpath=os.path.join(root,name)   #形成完整路径

        if (('.jpg' in newpath) or ('.JPG' in newpath))and('胰腺' in newpath) and ('DICOM' not in newpath):
            s=newpath.split('/')   #分割路径的每一级文件夹名称,存在s[i]中
            jpgname = 'xxx'
            for i in range(len(s)):     #遍历s[i],按照每一级文件夹不同的命名规律修改if条件
                if 'yixian' in s[i]:
                    print(s[i+1])
                    jpgname=s[i+1]
                if '' in s[i]:
                    print(s[i])
                    jpgname=jpgname+'_'+s[i]
                if (('标记' in s[i]) or 'JBJ' in s[i]) and s[i+1]!='加标记':
                    print(s[i])
                    jpgname=jpgname+'_'+s[i]
                    #os.rename(newpath, os.path.join(dstdir1, jpgname))
                if (('JPG' in s[i])or ('jpg' in s[i] )or ('JPg' in s[i])) and '.JPG' not in s[i] and '.jpg' not in s[i] and(s[i+1]!='JPG'):
                    print(s[i])
                    jpgname=jpgname+'_'+s[i-1]+'_'+s[i]
                    #os.rename(newpath, os.path.join(dstdir0, jpgname))
                if 'majl' in s[i]:
                    print(s[i])
                    jpgname=jpgname+'_'+s[i]
                if ('.jpg' in s[i]) or ('.JPG' in s[i]):
                    jpgname=jpgname+'_'+s[i]
            jpgname = jpgname + '.jpg'
            if '标记' in newpath:
                os.rename(newpath, os.path.join(dstdir1,jpgname))
            else:
                os.rename(newpath, os.path.join(dstdir0,jpgname))

你可能感兴趣的:(python)