import os
import shutil
def isexist(name, path=None):
'''
:param name: 需要检测的文件或文件夹名
:param path: 需要检测的文件或文件夹所在的路径,当path=None时默认使用当前路径检测
:return: True/False 当检测的文件或文件夹所在的路径下有目标文件或文件夹时返回Ture,
当检测的文件或文件夹所在的路径下没有有目标文件或文件夹时返回Falsefin
'''
if path is None:
path = os.getcwd()
if os.path.exists(path + '/' + name):
print("Under the path: " + path + '\n' + name + " is exist")
return True
else:
if (os.path.exists(path)):
print("Under the path: " + path + '\n' + name + " is not exist")
else:
print("This path could not be found: " + path + '\n')
return False
img_root = "图片文件夹路径"
count = 0
txtsroot = "txt文件夹路径"
save_folder = "即将要保存的文件夹"
if not os.path.exists(save_folder):
os.mkdir(save_folder)
fileList = os.listdir(img_root)
for img_path in fileList:
picsname = os.path.basename(img_path)
txtname = picsname.replace(".jpg", ".txt")
txt_path = os.path.join(txtsroot, txtname)
if isexist(txtname, txtsroot):
count += 1
new_file_path = os.path.join(save_folder, txtname)
shutil.move(txt_path, new_file_path)
print("finish!", count)
根据图片文件夹中图片名找txt文件夹中对应txt文件,找到后存储于新文件夹中
最新推荐文章于 2024-09-23 14:24:34 发布