Search code examples
pythonargumentsparameter-passingcommand-line-arguments

How to schedule python script that accepts arguments via input()


I have a python 3.11 script that prompts for input() and stores the responses as vars. I have been running the script interactively within my IDE.

Something like: [I click run for script.py]

Enter a date:
[I enter 2023-08-09]
Enter a geography id:
[I enter 18]

Now I need to be able to run from command line and schedule the script. So I need to run something like: python script.py -2023-08-09 -18

Should I create a new version of the script that uses arguments via sys.argv and argparse, or is there a better way to adapt my script to use input() when run interactively but also send arguments via command line when run from terminal?


Solution

  • Best would probably be to change the script to use argparse. It can fall back to prompting if the are no command-line arguments.

    But you can use a heredoc to feed input to the script. Put this in a shell script:

    #!/bin/sh
    
    python script.py <<EOF
    2023-08-09
    18
    EOF
    

    and use cron or at to schedule the script.