STDIN vs Command line arguments


  • First try this; Then repeate. We must type in the same path again and again. Boring and error-prone.


examples/basics/convert_stdin.py

input_file = input("Input file: ")
output_file = input("Output file: ")

print(f"This code will read {input_file}, analyze it and then create {output_file}")
...
  • We could use a Tk-based dialog:
  • Still boring (though maybe less error-prone)


examples/basics/convert_with_tk_dialog.py

from tkinter import filedialog

# On recent versions of Ubuntu you might need to install python3-tk in addition to python3 using
# sudo apt-get install python3-tk

input_file = filedialog.askopenfilename(filetypes=(("Excel files", "*.xlsx"), ("CSV files", "*.csv"), ("Any file", "*")))
output_file = filedialog.asksaveasfilename(filetypes=(("Excel files", "*.xlsx"), ("CSV files", "*.csv"), ("Any file", "*")))

print(f"This code will read {input_file}, analyze it and then create {output_file}")
...
  • The command line has
  • History!


examples/basics/convert_argv.py

import sys

if len(sys.argv) != 3:
    exit(f"Usage: {sys.argv[0]} INPUT_FILE OUTPUT_FILE")

input_file = sys.argv[1]
output_file = sys.argv[2]

print(f"This code will read {input_file}, analyze it and then create {output_file}")
...