-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcopy_split.py
More file actions
55 lines (43 loc) · 1.9 KB
/
Copy pathcopy_split.py
File metadata and controls
55 lines (43 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import tqdm
import argparse
from pathlib import Path
import shutil
def copy_files_preserve_structure(filelist_path, root_folder, target_root):
filelist_path = Path(filelist_path)
root_folder = Path(root_folder).resolve()
target_root = Path(target_root).resolve()
if not filelist_path.exists():
raise FileNotFoundError(f"Filelist not found: {filelist_path}")
with open(filelist_path, 'r', encoding='utf-8') as f:
lines = f.readlines()
copied = 0
for line in tqdm.tqdm(lines):
line = line.strip()
if not line:
continue
try:
filepath_str, _, _, _ = line.split("|", 3)
except ValueError:
print(f"Skipping malformed line: {line}")
continue
full_src = Path(filepath_str).resolve()
if not full_src.exists():
print(f"❌ Source file not found: {full_src}")
continue
try:
rel_path = full_src.relative_to(root_folder)
except ValueError:
print(f"❌ File {full_src} is not under the root folder {root_folder}")
continue
dst = target_root / rel_path
dst.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(full_src, dst)
copied += 1
print(f"\n✅ Completed copying {copied} entries to: {target_root}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Copy files from filelist to a new folder, preserving directory structure.")
parser.add_argument("--filelist_path", type=str, required=True, help="Path to the filelist.txt")
parser.add_argument("--root_folder", type=str, required=True, help="Root folder of original data")
parser.add_argument("--target_folder", type=str, required=True, help="Root directory to copy files into")
args = parser.parse_args()
copy_files_preserve_structure(args.filelist_path, args.root_folder, args.target_folder)