STDIN vs Command line arguments


If we run this script without any command-line parameters it will print out usage information.

If we give it two parameters it will treat the first one as the name of an input file and the second as the name of an output file.


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}")
...

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}")

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}")
...