From 53d122a5cf999df5bc596a620d3ae40f3486f10f Mon Sep 17 00:00:00 2001 From: Polarolouis Date: Thu, 27 Oct 2022 10:54:10 +0200 Subject: [PATCH] Added blank line remover script for text files --- text-scripts/blank-lines-remove.py | 42 ++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 text-scripts/blank-lines-remove.py diff --git a/text-scripts/blank-lines-remove.py b/text-scripts/blank-lines-remove.py new file mode 100644 index 0000000..563abaa --- /dev/null +++ b/text-scripts/blank-lines-remove.py @@ -0,0 +1,42 @@ +#!/usr/bin/python3 + +# A script to parse all text files in a directory and remove blank lines +# Copyright (C) 2022 Louis Lacoste + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +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") + +args = parser.parse_args() + +path = Path(args.directory) +textFiles = list(path.glob("*.txt")) +print(textFiles) + +for textFile in textFiles: + if textFile.exists() and not textFile.is_dir(): + fileContentString = "" + fileContentList = [] + 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: + for line in fileContentList: + currentTextFile.write(f"{line}\n") \ No newline at end of file