-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadding_strings_to_filenames.py
80 lines (74 loc) · 2.83 KB
/
adding_strings_to_filenames.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
import os
import fnmatch
import traceback
import sys
def GuessDirSeparator(strpath):
"""return a directory separator likely used in strpath, return os.sep if no separator seems to be used"""
osep = os.sep
if(strpath.find(osep)>=0):
return osep
if(strpath.find("/")>=0):
return "/"
if(strpath.find("\\")):
return "\\"
return osep
def AddStringsToFileName(filepath=r"path\to\a\file.ext", addbefore="", addafter=""):
"""Return filepath so that r"path\to\a\file.ext" is returned as r"path\to\a\"+addbefore+"file"+addafter+"ext"""
separator = GuessDirSeparator(filepath)
base = os.path.basename(filepath)
dname = os.path.dirname(filepath)
basesplit = base.split(".")
filename = basesplit[0]
extension = ""
if(len(basesplit)>0):
extension = "." + ".".join(basesplit[1:])
filename = addbefore + filename + addafter + extension
return os.path.join(dname, filename)
def ListFiles(pattern, topdir):
import fnmatch
for path, dirs, files in os.walk(os.path.abspath(topdir)):
for filename in fnmatch.filter(files, pattern):
yield os.path.join(path, filename)
def main(top = r"C:\path\to\toppath", pattern = "*", pre = "before_", post = "_after", logpath = r"C:\temp\logme.txt"):
"""
Rename all files under *top* folder (recursively) that match *pattern* so that the new file name is *pre*+originalfilename+*post*+.ext.
top: root folder to start in
pattern: rename only files that match this pattern, use e.g. *.jpg to deal with jpg files only, use * to include all files
pre: string to append at the beginning of a file name
post: string to append at the end of a file name
logpath: path to a logfile
"""
try:
logsep = "\t"
errorfreerun = True
logfile = open(logpath, "w")
#
ff = ListFiles(pattern, top)
for f in ff:
#print "renaming: " + str(f)
try:
newname = AddStringsToFileName(f, pre, post)
logfile.write(f + logsep)
os.rename(f, newname) # rename it
logfile.write(newname)
except Exception, e:
logfile.write("Error: " + str(e))
errorfreerun = False
finally:
logfile.write("\n")
#
if not errorfreerun:
print "There were some errors! See "+ logpath + " for details!"
#
except:
tb = sys.exc_info()[2]
tbinfo = traceback.format_tb(tb)[0]
pymsg = tbinfo + "\n" + str(sys.exc_type)+ ": " + str(sys.exc_value)
print pymsg
finally:
if "logfile" in dir():
logfile.close()
if __name__ == '__main__':
pass
#example use:
#main(top = r"C:\path\to\top\dir", pattern = "*", pre = "before_", post = "_after", logpath = r"C:\temp\logme.txt")