Refactored with autopep8, added verbose mode
This commit is contained in:
parent
53d122a5cf
commit
cd442ad124
1 changed files with 14 additions and 7 deletions
|
|
@ -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")
|
||||
currentTextFile.write(f"{line}\n")
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue