Next Spaceship

Driving into future…

Replace Strings for All Files in a Folder

| Comments

In my previous blog, I wrote the code to replace all string in a file: http://leons.im/2011/02/replace-all-strings-in-a-file/. Today, I write a cooler code – It can replace all strings in many files, actually it can replace all strings in files that are in a same folder.

Save the following code to the folder that you want to replace strings, and name it like replace.py.

Modify parameters from_str, to_str and files_to_replace in the form of regular expression. Then run python replace.py.

Replace Strings for All Files in a Folder
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
#!/usr/bin/python
import sys, os, re
#Created by Leon on Aug 9, 2012

from_str = r'\t'
to_str = r'    '
files_to_replace = r'.(h|m|mm|cpp|inl|def|txt|php|tpl|css|js|py)$'

def replace(infile, outfile):
    f1 = open(infile)
    f0 = open(outfile, 'w')
    data = f1.readlines()
    for line in data:
        f0.write(re.sub(from_str, to_str, line))
    f1.close()
    f0.close()

def walk_folder(path):
    print "Current Path: " + path
    for dirpath, dirs, files in os.walk(path):
        for filename in files:
            if re.search(files_to_replace, filename):
                print "----" + filename + "...."
                fi = open(os.path.join(dirpath, filename))
                fo = open(os.path.join(dirpath, filename + ".bk"), 'w')
                fo.write(fi.read())
                fo.close()
                fi.close()
                replace(os.path.join(dirpath, filename + ".bk"),
                          os.path.join(dirpath, filename))
                print "Done."
                os.remove(os.path.join(dirpath, filename + ".bk"))

if __name__ == '__main__':
    path = os.path.abspath(os.path.dirname(sys.argv[0]))
    walk_folder(path)
    print
    print "All files have been translated successfully."
    print
    print "Created for you by Leon on Aug 9, 2012."
    raw_input()

Comments