-
Notifications
You must be signed in to change notification settings - Fork 32
/
low2upper.py
83 lines (70 loc) · 2.57 KB
/
low2upper.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python3
# coding=utf-8
"""Rename filename, lower to upper.
:author: Jon Jiang
:email: jiangyingming@live.com
"""
from textwrap import shorten
import argparse
import glob
import itertools
import os
import shutil
import sys
def low2upper(src_file, out_dir, keep_src):
"""Rename filename, lower to upper:
1. If out_dir is None, rename original file;
2. If out_dir is not None and keep_src is true, rename using copy;
3. If out_dir is not None and keep_src is false, rename using move.
"""
src_dir, filename = os.path.split(src_file)
if out_dir is None:
dst_file = os.path.join(src_dir, filename.upper())
else:
dst_file = os.path.join(out_dir, filename.upper())
if keep_src:
shutil.copy2(src_file, dst_file)
else:
shutil.move(src_file, dst_file)
def init_args():
"""Initilize function, parse user input."""
# initilize a argument parser
parser = argparse.ArgumentParser(
description="Rename filename, lower to upper."
)
# add arguments
parser.add_argument('-v', '--version', action='version',
version='%(prog)s 0.2.1')
parser.add_argument('-k', '--keep', action='store_true',
help='keep original file')
parser.add_argument('-r', '--recursive', action='store_true',
help='search file recursively')
parser.add_argument('-out', metavar='<directory>', default=None,
help='output directory, [default: original folder]')
parser.add_argument('files', metavar='<file>', nargs='+',
help='file will be processed')
return parser.parse_args()
def main():
"""Main function."""
args = init_args()
globstrs, out_dir = args.files, args.out
keep_src, recursive = args.keep, args.recursive
# if the out_dir is None and --keep is setted, process as an error
if keep_src and out_dir is None:
err_message = 'Error! Blank output directory is conflict with --keep.'
print(err_message, file=sys.stderr)
return 1
# start process
if out_dir is not None:
os.makedirs(out_dir, exist_ok=True)
# collect input globstrs into a glob list
print('Start processing: {}'.format(shorten(', '.join(globstrs), 62)))
globs = [glob.iglob(globstr, recursive=recursive) for globstr in globstrs]
count = 0
for src_file in itertools.chain(*globs):
low2upper(src_file, out_dir, keep_src)
count += 1
print('{} files have been processed.'.format(count))
return 0
if __name__ == '__main__':
main()