Removing Six.b From Multiple Files
I have dozens of files in the project and I want to change all occurences of six.b('...') to b'...'. Can I do that with some sort of regex bash script?
Solution 1:
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)
withopen(fname) as f:
txt = f.read()
txt = re.sub(r'six\.(b\("[^"]*"\))', r'\1', txt)
withopen(fname, 'w') as f:
f.write(txt)
print(fname)
Solution 2:
A relatively simple bash solution (change *.foo to *.py or whatever filename pattern suits your situation):
#!/bin/bashexport FILES=`find . -type f -name '*.foo' -exec egrep -l 'six\.b\("[^\"]*"\)' {} \; 2>/dev/null`
for file in$FILESdocp$file$file.bak
sed 's/six\.b(\(\"[^\"]*[^\\]\"\))/b\1/'$file.bak > $fileecho$filedone
Notes:
It will only consider/modify files that match the pattern
It will make a '.bak' copy of each file it modifies
It won't handle embedded
\")
, e.g.six.b("asdf\")")
, but I don't know that there is a trivial solution to that problem, without knowing more about the files you're manipulating. Is the end ofsix.b("")
guaranteed to be the last")
on the line? etc.
Post a Comment for "Removing Six.b From Multiple Files"