-
Notifications
You must be signed in to change notification settings - Fork 5
/
filename_renamer.py
70 lines (58 loc) · 2.18 KB
/
filename_renamer.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
# Copyright (c) 2021 W-Mai
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
#
# filename_renamer/filename_renamer.py
# Created by W-Mai on 2021/3/14.
# repo: https://github.com/W-Mai/filename_renamer
#
##############################################################
import argparse
import os
import time
Suffix = [".bin"]
def CheckAllowed(filepath):
path_split = os.path.splitext(filepath)
suffix: str = path_split[-1]
return suffix.lower() in Suffix
def ConvOneFile(filepath, target_name):
try:
root_path = os.path.dirname(filepath)
target_file_path = os.path.join(root_path, target_name)
os.rename(filepath, target_file_path)
except Exception as e:
print(e)
return 'NOT ALLOWED'
return 'SUCCESS'
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('filepath', type=str, nargs="+",
help='images dir paths (or file paths) you wanna convert')
parser.add_argument('-o', '-output-filename', type=str,
help='output file name.')
parser.add_argument('-s', '-suffix', type=str, nargs="+",
help='file suffix.')
args = parser.parse_args()
if args.s:
Suffix = args.s
file_count = 0
for path in args.filepath:
if os.path.isdir(path):
for root, dirs, files in os.walk(path):
# if root == path:
# break
for item in files:
abs_path = os.path.abspath(os.path.join(root, item))
if not CheckAllowed(abs_path): continue
print(f'{file_count:<5} {abs_path} START', end='')
t0 = time.time()
conv_rtn = ConvOneFile(abs_path, args.o)
if conv_rtn == "SUCCESS":
file_count += 1
print('\b' * 5 + 'FINISHED', end='')
elif conv_rtn == "NOT ALLOWED":
print('\b' * 5, end='')
print(f' {(time.time() - t0) * 1000} ms')
print()
print(f"Convert Complete. Total rename {file_count} file(s).")