Last active
June 18, 2022 16:07
-
-
Save lixingcong/4052bbcab5d8320b883945b060ae7e93 to your computer and use it in GitHub Desktop.
深度学习,递归地移动某目录下所有文件到别处,用于划分测试集
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
# | |
# 递归移动某个文件夹下的所有文件到另一个文件,作为深度学习划分测试集 | |
# 2022-06-18 | |
# | |
# 参数: | |
# python3 main.py -s <SRC-DIR> -d <DEST-DIR> -p <MOVE-PERCENT> | |
# 例子: | |
# python3 main.py E:\image-src E:\image-dest -p 90 | |
import shutil | |
import os | |
import argparse | |
import sys | |
import random | |
g_dirty_run = False | |
# src_dir源目录,如E:\image-src | |
# dest_dir目标目录,如E:\image-dest | |
# percent需要移动文件百分比,对文件进行移动,对目录递归创建目录 | |
def move_files(src_dir, dest_dir, percent): | |
if not os.path.exists(dest_dir): | |
os.makedirs(dest_dir) | |
listdir_results = os.listdir(src_dir) | |
src_filenames = [] # 本目录下的文件 | |
for listdir in listdir_results: | |
src_filename = os.path.join(src_dir, listdir) | |
if os.path.isfile(src_filename): | |
src_filenames.append(src_filename) | |
elif os.path.isdir(src_filename): | |
dest_filename = os.path.join(dest_dir, listdir) | |
move_files(src_filename, dest_filename, percent) | |
src_file_count = len(src_filenames) | |
if 0 == src_file_count: | |
return | |
move_count = int(src_file_count * percent) | |
print('src dir={0}, {1}/{2} files to be moved'.format(src_dir, move_count, src_file_count)) | |
if g_dirty_run is False and move_count > 0: | |
random.shuffle(src_filenames) | |
#print(src_filenames) | |
src_filenames = src_filenames[:move_count] | |
for src_filename in src_filenames: | |
shutil.move(src_filename, dest_dir) | |
if __name__ == "__main__": | |
script_file = os.path.basename(sys.argv[0]) | |
parser = argparse.ArgumentParser(prog=script_file, | |
formatter_class=lambda prog: argparse.HelpFormatter(prog,max_help_position=80), | |
description='A simple file mover to split dataset to test') | |
parser.add_argument('-s','--src-dir', help='source dir', required=True) | |
parser.add_argument('-d','--dest-dir', help='dest dir', required=True) | |
parser.add_argument('-p','--move-percent', help='move percent, range is (1,100]', required=True) | |
parser.add_argument("--dirty-run", help="run for brief preview, would not move any files", action="store_true") | |
args = vars(parser.parse_args()) | |
src_dir, dest_dir, move_percent, g_dirty_run = args['src_dir'], args['dest_dir'], float(args['move_percent']), args['dirty_run'] | |
if g_dirty_run: | |
print('Dirty run mode, no files will be moved, but new folders will be created') | |
if move_percent > 0 and move_percent <= 100: | |
move_files(src_dir, dest_dir, move_percent / 100) | |
else: | |
print('Error, move percent must in range (1,100]') | |
sys.exit(-1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment