Browse Source

Add read and write in the same file

Signed-off-by: Rémi BERTHO <remi.bertho@dalan.fr>
master
Rémi BERTHO 4 years ago
parent
commit
1c8404e029
Signed by: dalan GPG Key ID: EE3B917931C07B64
  1. 53
      SSnR.py

53
SSnR.py

@ -41,11 +41,11 @@ def main():
parser.add_argument('-o', '--output', help='Output file', required=False)
parser.add_argument('-r', '--replace', help='Replace', required=False)
parser.add_argument('-m', '--print_nb_match', help='Print the number of match in replace',
required=False, action='store_true')
required=False, action='store_true')
parser.add_argument('-c', '--ignore_case', help='Ignore the case',
required=False, action='store_true')
required=False, action='store_true')
parser.add_argument('-u', '--recursive', help='Use the regex input recrusivly in the folders',
required=False, action='store_true')
required=False, action='store_true')
args = vars(parser.parse_args())
@ -60,7 +60,7 @@ def main():
return -1
# Get input
filenames = []
input_filenames = []
if args["regex_input"] is not None:
try:
input_ex = compile_regex(args["regex_input"], False)
@ -73,21 +73,21 @@ def main():
for (dirpath, dirnames, dir_filenames) in walk("."):
for filename in dir_filenames:
if input_ex.fullmatch(filename):
filenames.append(os.path.join(dirpath, filename))
input_filenames.append(os.path.join(dirpath, filename))
is_file = True
if not args["recursive"]:
break
if not filenames:
if not input_filenames:
print("Error: no input file")
return -1
elif args["input"] is not None:
for input_file in args["input"]:
if os.path.isfile(input_file):
is_file = True
filenames.append(input_file)
input_filenames.append(input_file)
else:
print("Error: file not found: " + str(args["input"]))
if not filenames:
if not input_filenames:
print("Error: no input file")
return -1
elif args["string"] is not None:
@ -98,27 +98,16 @@ def main():
is_file = False
# Get output
output_files = []
output_filenames = []
if args["regex_output"] is not None:
if input_ex is None:
print("Error: You need a regex input file to use a regex output file")
return -1
for input_filename in filenames:
try:
output_file = open((input_ex.subn(args["regex_output"], input_filename))[0], "w",
encoding="utf8")
output_files.append(output_file)
except OSError as exception:
print("Error: file not found: " + str(exception))
return -1
for input_filename in input_filenames:
output_filenames.append(input_ex.subn(args["regex_output"], input_filename)[0])
use_output_file = True
elif args["output"] is not None:
try:
output_file = open(args["output"], "w", encoding="utf8")
output_files.append(output_file)
except OSError as exception:
print("Error: file not found: " + str(exception))
return -1
output_filenames.append(args["output"])
use_output_file = True
else:
use_output_file = False
@ -126,7 +115,7 @@ def main():
# Search or replace
file_index = 0
if is_file:
for filename in filenames:
for filename in input_filenames:
try:
with open(filename, "r", encoding="utf8") as file:
string = file.read()
@ -140,10 +129,15 @@ def main():
if args["replace"] is not None:
replace_string, nb_replace = replace(ex, string, args["replace"])
if use_output_file:
output_files[file_index].write(replace_string)
try:
output_file = open(output_filenames[file_index], "w", encoding="utf8")
except OSError as exception:
print("Error: file not found: " + str(exception))
return -1
output_file.write(replace_string)
print("File: " + filename)
print(" - Number of replace: " + str(nb_replace))
if len(output_files) > 1:
if len(output_filenames) > 1:
file_index += 1
else:
print(replace_string)
@ -161,7 +155,12 @@ def main():
replace_string, nb_replace = replace(ex, string, args["replace"])
if use_output_file:
print("Number of replace: " + str(nb_replace))
output_files[0].write(replace_string)
try:
output_file = open(output_filenames[file_index], "w", encoding="utf8")
except OSError as exception:
print("Error: file not found: " + str(exception))
return -1
output_file.write(replace_string)
else:
print(replace_string)
if args["print_nb_match"]:

Loading…
Cancel
Save