-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlreplace.py
executable file
·62 lines (46 loc) · 1.62 KB
/
lreplace.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
#!/usr/bin/python3
# Name: lreplace
# Version: 1.1.0
# Description: Replaces file name that begins with a number with a string.
# Author: LeonardoM011
# Date: 1/9/2019
import os
import argparse
program_description = \
"Replaces file name that begin with number with some text."
program_version = "%(prog)s 1.1.0"
def replace_numbers(name, replace_with, incl_period):
while True:
if name[0].isdigit() \
or (incl_period is True and name[0] == '.'):
name = name[1:]
continue
break
name = replace_with + name
return name
def main():
parser = argparse.ArgumentParser(description=program_description)
path_help = "Path to folder for files to rename."
parser.add_argument(
'path', type=str, nargs=1, help=path_help)
text_help = "Text that replaces numbers."
parser.add_argument(
'text', type=str, nargs=1, help=text_help)
period_help = "Weather or not to replace period too."
parser.add_argument(
'-s', '--include-period', action='store_true', help=period_help)
parser.add_argument(
'-v', '--version', action='version', version=program_version)
args = parser.parse_args()
path = args.path[0]
text = args.text[0]
include_period = args.include_period
for filename in os.listdir(path):
if filename[0].isdigit():
newname = replace_numbers(
filename, text, include_period)
filename = path + '/' + filename
newname = path + '/' + newname
os.rename(filename, newname)
if __name__ == '__main__':
main()