Repository navigation
Expand file tree
/
Copy pathrenameScript.py
More file actions
85 lines (72 loc) · 2.54 KB
/
Copy pathrenameScript.py
File metadata and controls
85 lines (72 loc) · 2.54 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
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
84
85
from os import listdir
import os.path
import sys
import getopt
import re
args = '-d -r -w'.split()
def main():
try:
opts, args = getopt.getopt(sys.argv[1:], "d:r:w:x:", ["dir=", "replace=", "with=", "ext="])
except getopt.GetoptError as err:
# print help information and exit:
print str(err) # will print something like "option -a not recognized"
## usage()
sys.exit(2)
directory = None
replaceStr = ""
newStr = []
oldStr = []
verbose = False
filesInDir = {}
filePrefix = ""
ext = ""
for o, a in opts:
if o in ("-d", "--dir"):
directory = a
print "dir: %s" %(directory)
elif o in ("-r", "--replace"):
replaceStr = a
elif o in ("-w", "--with"):
newStr = a
elif o in ("-x", "--ext"):
ext = a
else:
assert False, "unhandled option"
if(not os.path.isdir(directory)):
print "dir fail!"
sys.exit(2)
if(ext == ""):
print "fail! no ext!"
sys.exit(2)
filesInDir = os.listdir(directory)
filesInDir.sort()
newFileList = []
newFileName = []
for f in filesInDir:
if f.endswith("." + ext):
## raw_input()
tmp = os.path.splitext(f)[0]
filePrefix, tmpOldStr = tmp.split("-", 1)
oldStr = tmpOldStr.split("-")
appendString = ""
newFileName.append(filePrefix)
if(len(oldStr[0]) < 2):
appendString = "0"
else:
appendString = ""
newFileName.append(appendString + oldStr[0])
if(len(oldStr[1]) < 2):
appendString = "-0"
else:
appendString = ""
newFileName.append(appendString + oldStr[1])
newFileName.append("." + ext)
## print(newFileName[0] + "-" + newFileName[1] + "-" + newFileName[2] + newFileName[3])
## newFileList.append(newFileName[0] + "-" + newFileName[1] + "-" + newFileName[2] + newFileName[3])
newFile = (newFileName[0] + "-" + newFileName[1] + "-" + newFileName[2] + newFileName[3])
os.rename(os.path.join(directory, f), os.path.join(directory, newFile))
newFileName = []
print "renaming %s to %s" %(os.path.join(directory, f), os.path.join(directory, newFile))
input = raw_input()
if __name__ == "__main__":
main()