Created
December 17, 2017 20:24
-
-
Save kaashmonee/8b585f300eace432fd94c160d267c095 to your computer and use it in GitHub Desktop.
Copies and shortens code from one file to another
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
def cp(inp, out): | |
with open(out, "w") as output, open(inp, "r") as inp: | |
lines = [] | |
for line in inp: | |
# ignores new lines | |
if line == "\n": | |
continue | |
# gets the first word | |
sentenceList = line.split(" ", 1) | |
firstWord = sentenceList[0] | |
# shortens the first word if it's longer than 4 | |
if len(firstWord) > 4: | |
firstWord = firstWord[0] | |
# replaces the shortened word | |
lines.append(firstWord+" "+" ".join(sentenceList[1:])) | |
output.writelines(lines) | |
def main(): | |
parser = argparse.ArgumentParser(description="Copies and shortens lines" | |
" between 2 files.") | |
parser.add_argument("inputFile", metavar="inputFile", help="Input file") | |
parser.add_argument("outputFile", metavar="outputFile", help="Output file") | |
args = parser.parse_args() | |
cp(args.inputFile, args.outputFile) | |
print("Copied from "+str(args.inputFile) + " to " + args.outputFile + ".") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment