contentreplace glob first argument.

This commit is contained in:
Ethan Dalool 2019-03-22 14:51:02 -07:00
parent 6a8a687288
commit 852504be13

View file

@ -1,6 +1,7 @@
import glob
import codecs import codecs
import sys import sys
filename = sys.argv[1] filenames = glob.glob(sys.argv[1])
replace_from = sys.argv[2] replace_from = sys.argv[2]
replace_to = sys.argv[3] replace_to = sys.argv[3]
try: try:
@ -11,25 +12,29 @@ except IndexError:
replace_from = codecs.decode(replace_from, 'unicode_escape') replace_from = codecs.decode(replace_from, 'unicode_escape')
replace_to = codecs.decode(replace_to, 'unicode_escape') replace_to = codecs.decode(replace_to, 'unicode_escape')
f = open(filename, 'r', encoding='utf-8') def contentreplace(filename):
with f: f = open(filename, 'r', encoding='utf-8')
content = f.read() with f:
content = f.read()
occurances = content.count(replace_from) occurances = content.count(replace_from)
if occurances == 0: if occurances == 0:
print('No occurances') print('No occurences')
exit() return
print('Found %d occurances.' % occurances) print('Found %d occurences.' % occurances)
if automatic: if automatic:
permission = 'y' permission = 'y'
else: else:
permission = input('Replace? ') permission = input('Replace? ')
if permission.lower() not in ['y', 'yes']: if permission.lower() not in ['y', 'yes']:
exit() exit()
content = content.replace(replace_from, replace_to) content = content.replace(replace_from, replace_to)
f = open(filename, 'w', encoding='utf-8') f = open(filename, 'w', encoding='utf-8')
with f: with f:
f.write(content) f.write(content)
for filename in filenames:
contentreplace(filename)