-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrm_whitespace.py
More file actions
executable file
·46 lines (39 loc) · 1.46 KB
/
rm_whitespace.py
File metadata and controls
executable file
·46 lines (39 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#! /usr/bin/env python
import argparse
import re
from sys import exit
def parse_lines(file, output):
# Remove whitespaces from the beginning of each line
# We put end="" on print because line also includes newline characters
for line in file:
print(re.sub(r"^\s+", r"", line), file=output, end="")
def main():
# We prepare argument parser and add necessary arguments
# inputfile - required, we will read lines from this file
# output - optional, we will write lines to this file
parser = argparse.ArgumentParser(
prog="remove_spaces",
description="Remove whitespaces from list of strings in given file",
epilog="Hope you enjoy"
)
parser.add_argument("inputfile")
parser.add_argument("-o","--output", required=False)
args = parser.parse_args()
# We will add this extension to the output file if it is not specified
default_file_extension = "_rm_whitespace"
# We open both input and output file (create if it doesn't exist)
# Exceptions are caught
# If everything goes well we call the parse_lines function
try:
with(
open(args.inputfile, mode="r") as f,
open(args.output or args.inputfile + default_file_extension, mode="w+") as t
):
parse_lines(f, t)
except FileNotFoundError:
parser.print_usage()
exit("\nFile not found.")
except Exception as e:
raise e
if __name__ == "__main__":
main()