python 文件太多打不开文件夹,取样少量的文件,
建立同样的文件目录结构
import os
import shutil
root_file = "/media/algo/data_1/everyday/20230203/snowy"
basename_ = os.path.basename(root_file)
dirname_ = os.path.dirname(root_file)
T_nums_sample = 500
# print("basename_=", basename_)
# print("dirname_=", dirname_)
basename_new = basename_ + "_sample"
new_path = os.path.join(dirname_, basename_ + "_sample")
print(new_path)
if not os.path.exists(new_path):
os.makedirs(new_path)
path_files = []
cnt_2 = 0
g = os.walk(root_file)
for path, d, filelist in g:
for filename in filelist:
if filename.endswith('.jpg'):
if "raw" in filename:
continue
print("cnt=", cnt_2, "filename=", filename)
cnt_2 += 1
path_file = os.path.join(path, filename)
path_new = path_file.replace(basename_, basename_new)
path_new_dir = os.path.dirname(path_new)
if not os.path.exists(path_new_dir):
os.makedirs(path_new_dir)
else:
num_ = len(os.listdir(path_new_dir))
if num_ < T_nums_sample:
shutil.copy(path_file, path_new_dir)
print(os.path.basename(path_new_dir), ":num=", num_)
标签:文件,python,basename,文件夹,file,path,new,dirname,os
From: https://www.cnblogs.com/yanghailin/p/17094232.html