python 文件太多打不开文件夹,取样少量的文件
python 文件太多打不开文件夹,取样少量的文件,
建立同样的文件目录结构
1. 在文件夹旁边新建文件夹
import os
import shutil
import random
root_file = "/media/algo/data_1/everyday/20230203-有大文件/snowy_sample"
basename_ = os.path.basename(root_file)
dirname_ = os.path.dirname(root_file)
T_nums_sample = 10
# 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:
if 0 == len(d) and 0 != len(filelist):
random.shuffle(filelist)
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_)
2. 可以指定存储路径
import os
import shutil
import random
root_file = "/media/algo/data_1/everyday/data11"
basename_ = os.path.basename(root_file)
dirname_ = os.path.dirname(root_file)
T_nums_sample = 25
# print("basename_=", basename_)
# print("dirname_=", dirname_)
basename_new = basename_ + "_sample"
dirname_ = "/media/algo/data_1/everyday/20230209/rain-test-show-data"
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:
if 0 == len(d) and 0 != len(filelist):
random.shuffle(filelist)
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)
path_new_dir = path_new_dir.replace(root_file, new_path)
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_)
好记性不如烂键盘---点滴、积累、进步!