-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopyfiles.py
More file actions
52 lines (42 loc) · 1.44 KB
/
copyfiles.py
File metadata and controls
52 lines (42 loc) · 1.44 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
# python 3
from __future__ import print_function
import os
import sys
import argparse
from shutil import copyfile
def main():
parser = argparse.ArgumentParser(
description="Copy files in batch from a CSV file (src,dst per line)."
)
parser.add_argument("renames", help="CSV file with lines: src,dst")
parser.add_argument("-f", "--force", action="store_true", help="Overwrite destination files if they exist")
args = parser.parse_args()
if args.force:
print("-f detected: will overwrite files")
with open(args.renames, "r") as f:
for x in f:
# Allow for empty lines in the input file -- only stop at end of file
if x is None or x.strip() == '':
continue
args_line = x.split(',')
if len(args_line) < 2:
print("Skipping malformed line:", x.strip())
continue
a = args_line[0].strip()
b = args_line[1].strip()
print(a, end=" ")
print(" --> ", end=" ")
print(b)
try:
copyfile(a, b)
except FileNotFoundError as e:
print(e)
except FileExistsError as e:
if args.force:
print("Overwriting {}".format(b))
os.remove(b)
copyfile(a, b)
else:
print(e)
if __name__ == "__main__":
main()