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
|
# usage:
# replace.py -d <directory> -f <find> -r <replace> -s <suffix>
#
# <directory> is a folder, with subfolders, containing <suffix> files. In each <suffix> file in the directory or its children
# replace <text> with <replace>
import sys, os, re
import optparse
def usage():
print """
usage:
replace.py -d [directory] -f [text] -r [replace] -s [suffix]
directory is a folder containing [suffix] files.
In each [suffix] file in the directory or its subfolders,
replace [text] with [replace]
"""
def main():
parser = optparse.OptionParser()
parser.add_option('-s', '--suffix', dest="suffix" )
parser.add_option('-d', '--directory', dest="directory")
parser.add_option('-f', '--find', dest="find" )
parser.add_option('-r', '--replace', dest="replace")
(options, args) = parser.parse_args()
if not options.directory:
parser.error("directory argument not given")
usage()
return
if not options.find:
parser.error("find argument not given")
usage()
return
if not options.replace:
parser.error("replace argument not given")
usage()
return
outputDir = options.directory
findtext = options.find
replacetext = options.replace
suffix = options.suffix
if not(outputDir):
usage()
return
if findtext.startswith('"') or findtext.startswith("'"):
findtext = findtext[1:-1]
if replacetext.startswith('"') or replacetext.startswith("'"):
replacetext = replacetext[1:-1]
print "replace text",findtext,"with",replacetext,"in", suffix,"files"
for root, dirs, files in os.walk(outputDir, topdown=False):
for filename in files:
if (filename.endswith(suffix)):
infile = open(os.path.join(root, filename), "r")
svg = infile.read();
infile.close();
rslt = svg.find(findtext)
if (rslt >= 0):
svg = svg.replace(findtext, replacetext)
outfile = open(os.path.join(root, filename), "w")
outfile.write(svg);
outfile.close()
print "{0}".format(os.path.join(root, filename))
if __name__ == "__main__":
main()
|