commit 1f01318ff043e2b25f87932976c555af6eb513fc Author: Justine Pelletreau Date: Wed Mar 2 16:26:27 2022 +0100 First diff --git a/README.md b/README.md new file mode 100644 index 0000000..b6de904 --- /dev/null +++ b/README.md @@ -0,0 +1,14 @@ +# Spaceify + +This is a quickly put together utility. It takes a folder, and tries to open eveyr text file in it. + +It then turns every tab (\t) into 4 spaces, beacause IT'S THE LAW. + +``` +usage: spaceify.py [-h] -f FOLDER + +options: + -h, --help show this help message and exit + -f FOLDER, --folder FOLDER + Folder to search in +``` diff --git a/spaceify.py b/spaceify.py new file mode 100755 index 0000000..543005c --- /dev/null +++ b/spaceify.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 +#coding: utf-8 +import os +import sys + +def get_args(): + '''Gets all the arguments passed to the script and returns them in a parse_args()-type object. + No args + Returns: + -args : an args object containing all the optional arguments passed to the script. + ''' + import argparse + + parser = argparse.ArgumentParser() + parser.add_argument("-f", "--folder", help = "Folder to search in", action="store", type=str, required=True) + + #Creating the args object + args=parser.parse_args() + + return args + +def spaceify(filepath): + """Spaceify the file given by the absolute path filepath. + """ + + with open(filepath, "r") as myfile: + myfile = myfile.read() + + myfile.replace("\t", " ") + with open(filepath, "w") as yourfile: + yourfile.write(myfile) + + +args = get_args() +look_folder = args.folder +if look_folder[-1] == "/": + look_folder = look_folder[:-1] + +for thing in os.listdir(look_folder): + fpath = f"{look_folder}/{thing}" + if os.path.isfile(fpath): + try: + print(f"Working on {fpath}") + spaceify(fpath) + except Exception as e: + print(f"Got exception {e} for {fpath}, sorry") diff --git a/testdir/1.c b/testdir/1.c new file mode 100644 index 0000000..cf6be24 --- /dev/null +++ b/testdir/1.c @@ -0,0 +1,2 @@ + iofjqofjiq + sfsef fsfesfe