-
Notifications
You must be signed in to change notification settings - Fork 284
/
Copy pathpytype_copy.py
57 lines (47 loc) · 1.38 KB
/
pytype_copy.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
46
47
48
49
50
51
52
53
54
55
56
57
#! /usr/bin/python -B
"""Copy a group of files from a source directory to a destination directory.
Usage:
copy.py -s <SOURCE_DIRECTORY> -d <DESTINATION_DIRECTORY> file1 [file2 ...]
"""
import argparse
import os
import shutil
import sys
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"-s", "--src_dir", type=str, required=True, help="The source directory."
)
parser.add_argument(
"-d",
"--dst_dir",
type=str,
required=True,
help="The destination directory.",
)
parser.add_argument(
"file_list",
metavar="FILE",
type=str,
nargs="+",
help="List of files to copy.",
)
args = parser.parse_args()
return args
def copy_file(src_dir, dst_dir, filename):
dst_file = os.path.join(dst_dir, filename)
dst_parent = os.path.dirname(dst_file)
if not os.path.exists(dst_parent):
# Create the intermediate directories if they do not exist
os.makedirs(dst_parent)
shutil.copy(os.path.join(src_dir, filename), dst_file)
def main():
args = parse_args()
if not os.path.exists(args.src_dir):
sys.exit(f"Source directory '{args.src_dir}' does not exist.")
if not os.path.exists(args.dst_dir):
sys.exit(f"Destination directory '{args.dst_dir}' does not exist")
for filename in args.file_list:
copy_file(args.src_dir, args.dst_dir, filename)
if __name__ == "__main__":
main()