contentreplace glob first argument.
This commit is contained in:
parent
6a8a687288
commit
852504be13
1 changed files with 24 additions and 19 deletions
|
@ -1,6 +1,7 @@
|
|||
import glob
|
||||
import codecs
|
||||
import sys
|
||||
filename = sys.argv[1]
|
||||
filenames = glob.glob(sys.argv[1])
|
||||
replace_from = sys.argv[2]
|
||||
replace_to = sys.argv[3]
|
||||
try:
|
||||
|
@ -11,25 +12,29 @@ except IndexError:
|
|||
replace_from = codecs.decode(replace_from, 'unicode_escape')
|
||||
replace_to = codecs.decode(replace_to, 'unicode_escape')
|
||||
|
||||
f = open(filename, 'r', encoding='utf-8')
|
||||
with f:
|
||||
def contentreplace(filename):
|
||||
f = open(filename, 'r', encoding='utf-8')
|
||||
with f:
|
||||
content = f.read()
|
||||
|
||||
occurances = content.count(replace_from)
|
||||
if occurances == 0:
|
||||
print('No occurances')
|
||||
exit()
|
||||
occurances = content.count(replace_from)
|
||||
if occurances == 0:
|
||||
print('No occurences')
|
||||
return
|
||||
|
||||
print('Found %d occurances.' % occurances)
|
||||
if automatic:
|
||||
print('Found %d occurences.' % occurances)
|
||||
if automatic:
|
||||
permission = 'y'
|
||||
else:
|
||||
else:
|
||||
permission = input('Replace? ')
|
||||
if permission.lower() not in ['y', 'yes']:
|
||||
if permission.lower() not in ['y', 'yes']:
|
||||
exit()
|
||||
|
||||
content = content.replace(replace_from, replace_to)
|
||||
content = content.replace(replace_from, replace_to)
|
||||
|
||||
f = open(filename, 'w', encoding='utf-8')
|
||||
with f:
|
||||
f = open(filename, 'w', encoding='utf-8')
|
||||
with f:
|
||||
f.write(content)
|
||||
|
||||
for filename in filenames:
|
||||
contentreplace(filename)
|
||||
|
|
Loading…
Reference in a new issue