-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrename_files.py
executable file
·42 lines (33 loc) · 1.02 KB
/
rename_files.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
#!/usr/bin/env python3
import sys
import os
import glob
import re
import shutil
from csv import DictReader
src_dir = sys.argv[1]
dst_dir = sys.argv[2]
CSV_FILE = "./taps_procs.csv"
pcs_to_taps_id = {}
taps_to_pcs_id = {}
with open(CSV_FILE) as fd:
dr = DictReader(fd)
for paper in dr:
pcs_to_taps_id[paper['PCS_ID']] = paper['PAPER ID']
taps_to_pcs_id[paper['PAPER ID']] = paper['PCS_ID']
os.makedirs(dst_dir, exist_ok=True)
for f in glob.glob(f"{src_dir}/*.pdf"):
pcs_id = re.match(r"^.*\/([a-z]+\d+)\.pdf", f)
if pcs_id:
pcs_id = pcs_id.group(1)
taps_id = re.match(r"^.*\/(\d+)\.pdf", f)
if taps_id:
taps_id = taps_id.group(1)
assert(not (taps_id and pcs_id))
assert(taps_id or pcs_id)
if taps_id:
print("Moving:", taps_id, taps_to_pcs_id[taps_id])
shutil.copy2(f, f"{dst_dir}/{taps_to_pcs_id[taps_id]}.pdf")
elif pcs_id:
print("Moving:", pcs_id, pcs_to_taps_id[pcs_id])
shutil.copy2(f, f"{dst_dir}/{pcs_to_taps_id[pcs_id]}.pdf")