ArekBulski ArekBulski - 1 year ago 90
Python Question

Removing six.b from multiple files

I have dozens of files in the project and I want to change all occurences of

. Can I do that with some sort of regex bash script?

Answer Source

It's possible entirely in Python, But I would first make a backup of my project tree, and then:

import re
import os

indir = 'files'

for root, dirs, files in os.walk(indir):
    for f in files:
        fname = os.path.join(root, f)
        with open(fname) as f:
            txt =
        txt = re.sub(r'six\.(b\("[^"]*"\))', r'\1', txt)
        with open(fname, 'w') as f:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download