-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmove.py
45 lines (38 loc) · 1.12 KB
/
move.py
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
"""Move each image listed in a JSON file to a new directory."""
import os
import shutil
import argparse
import json
# Get Inputs
parser = argparse.ArgumentParser()
parser.add_argument("--json", help="the json file of images", required=True)
parser.add_argument("--src", help="the source directory", required=True)
parser.add_argument("--dest", help="the destination directory", required=True)
parser.add_argument(
"--copy", help="copy image files instead of moving", action="store_true"
)
args = parser.parse_args()
print(args)
# Get Files
file = []
data = json.load(open(args.json, "r"))
if isinstance(data, list):
files = data
elif isinstance(data, dict):
files = list(data.keys())
else:
raise Exception(f"File has unsupported data (list or dict only) {args.json}")
print(files)
# Move Files
os.mkdir(args.dest)
for file in files:
src = os.path.abspath(os.path.join(args.src, file))
print(file)
dest = os.path.abspath(os.path.join(args.dest, file))
try:
if args.copy:
shutil.copy(src, dest)
else:
shutil.move(src, dest)
except FileNotFoundError:
continue