Command-Line Interfaces (CLIs) are one of the best ways of providing your programs with useful parameters to customize their execution. If you are not familiar with CLI, in this blog post we will introduce them. Let’s say that you have a program that reads a file, computes something, and then, writes the results into another file. The simplest way of providing those arguments would be:
$ python mycode.py my/inputFile my/outputFile
### mycode.py ### def doSomething(inputFilename): with open(inputFilename) as f: return len(f.readlines()) if __name__ == "__main__": #Notice that the order of the arguments is important inputFilename = sys.argv[1] outputFilename = sys.argv[2] with open(outputFilename, "w") as f: f.write( doSomething(inputFilename))Continue reading