Python realizes to find (copy/cut) different types of files with the same name according to the file name

When making a data set for target detection, I must make the image files (jpg) of the training set and the annotation files (xml or json, shp, etc.) have the same file names and the same number of files. The prediction results are based on the correct results selected. , To find out other types of related files together, the amount of processed data is relatively large, so I wrote a few lines of simple code to filter out files with the same file name but different types (file suffixes), which is very convenient , Then be a share.


#根据挑选的jpg文件找出对应的xml、json、shp、dbf、shx文件
import os 
import shutil

filepath1 = '/opt/netdisk/object_detection/project/results/case2/xml'#源文件做参考
file_list = os.listdir(filepath1)
print(file_list)
print(len(file_list))

filepath2 = '/opt/netdisk/object_detection/project/results/case2/jpg'#需要拷出的文件位置
filepath3 = '/opt/netdisk/1/object_detection/project/results/case2/jpgnew'#拷入新的文件夹
print(len(os.listdir(filepath2)))

def main():
    n=0
    for file in os.listdir(filepath2):
        aa,bb=file.split('x')
        b = aa +'jpg'
        print(b)
        if b in file_list:
            srcfile = filepath2 +'/'+ file
            dstfile = filepath3 +'/'+ file
            shutil.move(srcfile,dstfile)#剪切功能
            shutil.copyfile(srcfile,dstfile)#拷贝出来
            n=n+1
            print(n)

if __name__ == '__main__':
    main()

filepath1 is the correct result obtained or the existing data needed, which is used for file name comparison.
filepath2 is the folder where
you want to copy or cut the file. filepath3 is the location where the copied or cut file is stored.

Guess you like

Origin blog.csdn.net/qq_44442727/article/details/112792866