Skip to content

Instantly share code, notes, and snippets.

@kaashmonee
Created December 17, 2017 20:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaashmonee/8b585f300eace432fd94c160d267c095 to your computer and use it in GitHub Desktop.
Save kaashmonee/8b585f300eace432fd94c160d267c095 to your computer and use it in GitHub Desktop.
Copies and shortens code from one file to another
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