To add an argument for the output file in Python, you can use the argparse
module to create a command-line interface. Here's an example:
main.py424 chars17 lines
In this example, the argparse
module is used to set up a command-line interface with an optional argument -o/--output
for specifying the output file. Then, the parse_args()
method is used to parse the command-line arguments into a Namespace
object.
Finally, the value of args.output
is checked to see if the output file was specified, and if so, open()
is used to write to the file. If the output file wasn't specified, a message is printed to the console.
You can run this script from the command line like this:
main.py35 chars2 lines
This will write the output to a file named output.txt
. If you don't specify the -o
argument, the script will print a message to the console instead of writing to a file.
gistlibby LogSnag