From cd442ad1245c1851bd630ddfe7ca3efb23d71327 Mon Sep 17 00:00:00 2001 From: Polarolouis Date: Thu, 27 Oct 2022 11:30:07 +0200 Subject: [PATCH] Refactored with autopep8, added verbose mode --- text-scripts/blank-lines-remove.py | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/text-scripts/blank-lines-remove.py b/text-scripts/blank-lines-remove.py index 563abaa..5de9fc5 100644 --- a/text-scripts/blank-lines-remove.py +++ b/text-scripts/blank-lines-remove.py @@ -20,23 +20,30 @@ import argparse from pathlib import Path parser = argparse.ArgumentParser() -parser.add_argument("directory", metavar="DIRECTORY", help="The directory in where are stored the text files") +parser.add_argument("directory", metavar="DIRECTORY", + help="The directory in where are stored the text files") +parser.add_argument("-v", "--verbose", action="store_true", + help="Enable verbose mode") args = parser.parse_args() path = Path(args.directory) textFiles = list(path.glob("*.txt")) -print(textFiles) +if args.verbose: + print("Text files:") + for textFile in textFiles: + print(f"- {textFile.name}") for textFile in textFiles: if textFile.exists() and not textFile.is_dir(): fileContentString = "" fileContentList = [] + if args.verbose: + print(f"\nRemoving empty lines for {textFile.name}") with textFile.open(encoding="utf8") as currentTextFile: fileContentString = currentTextFile.read() - fileContentList = [line for line in fileContentString.split('\n') if line.strip()] - print(fileContentString) - print(fileContentList) - with textFile.with_name(textFile.name+"noemptylines").open(mode="w", encoding="utf8") as currentTextFile: + fileContentList = [ + line for line in fileContentString.split('\n') if line.strip()] + with textFile.with_name(textFile.name[:-4] + "-noemptylines" + textFile.name[-4:]).open(mode="w", encoding="utf8") as currentTextFile: for line in fileContentList: - currentTextFile.write(f"{line}\n") \ No newline at end of file + currentTextFile.write(f"{line}\n")