scan_raw_dataset / split_files.py
larryshaw0079's picture
Upload split_files.py with huggingface_hub
be52b28 verified
Raw History Blame Contribute Delete
5.2 kB
import os
import shutil
import argparse
# 预定义的文件类型列表
FULL_FILETYPES = [
'.sens', '.txt',
'_vh_clean.ply', '_vh_clean_2.ply',
'_vh_clean.segs.json', '_vh_clean_2.0.010000.segs.json',
'.aggregation.json', '_vh_clean.aggregation.json',
'_vh_clean_2.labels.ply',
'_2d-instance.zip', '_2d-instance-filt.zip',
'_2d-label.zip', '_2d-label-filt.zip'
]
TEST_FILETYPES = [
'.sens', '.txt', '_vh_clean.ply', '_vh_clean_2.ply'
]
def split_files(source_dir, target_base_dir, file_types, move=False):
"""
将文件按后缀名分割到不同的文件夹中
参数:
source_dir: 源目录路径 (scans 或 scans_test)
target_base_dir: 目标基础目录路径
file_types: 要处理的文件类型列表
move: 是否移动文件而不是复制
"""
# 确保目标基础目录存在
if not os.path.exists(target_base_dir):
os.makedirs(target_base_dir)
# 为每种文件类型创建目标目录
for ext in file_types:
ext_dir = os.path.join(target_base_dir, ext.replace('.', '').replace('_', ''))
if not os.path.exists(ext_dir):
os.makedirs(ext_dir)
# 统计处理的文件数量
processed_count = 0
# 遍历源目录中的所有子文件夹(场景文件夹)
for scene_name in os.listdir(source_dir):
scene_dir = os.path.join(source_dir, scene_name)
# 确保是目录而不是文件
if os.path.isdir(scene_dir):
# 处理场景文件夹中的文件
for file in os.listdir(scene_dir):
# 检查文件类型是否在指定列表中
for ext in file_types:
if file.endswith(ext):
src_file = os.path.join(scene_dir, file)
# 创建目标目录结构
ext_dir = os.path.join(target_base_dir, ext.replace('.', '').replace('_', ''))
scene_target_dir = os.path.join(ext_dir, scene_name)
if not os.path.exists(scene_target_dir):
os.makedirs(scene_target_dir)
# 移动或复制文件
dst_file = os.path.join(scene_target_dir, file)
if move:
shutil.move(src_file, dst_file)
print(f"移动: {src_file} -> {dst_file}")
else:
shutil.copy2(src_file, dst_file)
print(f"复制: {src_file} -> {dst_file}")
processed_count += 1
break # 找到匹配的后缀后跳出内层循环
return processed_count
def main():
parser = argparse.ArgumentParser(description='将文件按后缀名分割到不同的文件夹中')
parser.add_argument('source_dir', choices=['scans', 'scans_test'],
help='源目录名称 (split_scans 或 split_scans_test)')
parser.add_argument('--target_dir', default='split_files',
help='目标目录名称,默认为 split_files')
parser.add_argument('--types', nargs='+',
help='要处理的文件类型,例如 .sens .txt')
parser.add_argument('--full', action='store_true',
help='使用完整的文件类型列表')
parser.add_argument('--test', action='store_true',
help='使用测试用的文件类型列表')
parser.add_argument('--move', action='store_true',
help='移动文件而不是复制文件')
# # 使用完整文件类型列表分割 scans 文件夹(移动文件)
# python split_files.py scans --full --move --target_dir split_scans
# # 使用测试文件类型列表分割 scans_test 文件夹(移动文件)
# python split_files.py scans_test --test --move --target_dir split_scans_test
# # 自定义文件类型分割 scans_test 文件夹(移动文件)
# python split_files.py scans_test --types .sens .txt --move
# # 指定目标目录名称(移动文件)
# python split_files.py scans_test --full --target_dir my_split_files --move
args = parser.parse_args()
# 确定要使用的文件类型列表
if args.types:
file_types = args.types
elif args.full:
file_types = FULL_FILETYPES
elif args.test:
file_types = TEST_FILETYPES
else:
# 默认使用完整列表
file_types = FULL_FILETYPES
print(f"将处理以下文件类型: {file_types}")
print(f"操作模式: {'移动' if args.move else '复制'}")
# 构建源目录路径
source_path = os.path.join(os.getcwd(), args.source_dir)
target_path = os.path.join(os.getcwd(), args.target_dir)
# 执行文件分割操作
count = split_files(source_path, target_path, file_types, args.move)
print(f"分割完成! 共处理了 {count} 个文件")
print(f"分割后的文件保存在: {target_path}")
if __name__ == "__main__":
main()