1

I have .py file having following lines of code

import pandas
import sys
cwd = os.chdir("n:/user/factory")
random_date = '2019-08-20'
# dataframe created
df = pd.DataFrame()
df['salary_date'] = random_date

I want to run this .py file through windows command prompt and control the 'random_date' from command prompt as date parameter. For example, if I want to change the above mentioned date to '2017-01-20', it should be changeable from the command prompt and run the code.

Arpit
  • 37
  • 5
  • 3
    Does this answer your question? [How to read/process command line arguments?](https://stackoverflow.com/questions/1009860/how-to-read-process-command-line-arguments) – mkrieger1 Feb 19 '21 at 21:25
  • Thanks for reply. Not exactly. Please suggest more. – Arpit Feb 19 '21 at 21:48
  • 1
    What is unclear to you after reading the answers to the other question? There isn't more to suggest. – mkrieger1 Feb 19 '21 at 22:18
  • 1
    @mkrieger To be fair, none of the answers really say how to *get* the supplied arguments. I've submitted a small edit for the accepted answer; hopefully it'll get accepted. – CrazyChucky Feb 19 '21 at 22:44

1 Answers1

0
import argparse
def get_arguments():

    parser = argparse.ArgumentParser()
    parser.add_argument("--date", help="Start Date in YYY-MM-DD format", required=False, default=None)
    
    return parser.parse_args()

args = get_arguments()

random_date = args.date 

# execute script normally but add "--date 2019-08-20" at the end 

fthomson
  • 773
  • 3
  • 9
  • Thanks for the reply. So this script needs to run in command prompt? – Arpit Feb 22 '21 at 03:20
  • If you are using PyCharm you can add them as parameters under run configuration to execute from IDE. Not sure about others. Another option is to have a conditional statements like if args.date: random_date = args.date else: random_date = "whatever date you want to test with" – fthomson Feb 22 '21 at 16:58