How to set the default value of the second args as the result of interpreting the first args in argparse?

53 Views Asked by At

I am making a user input part using argparse lib, python.

First argument is default woking directory path, and 2nd arguments are data file name. The data file is usually located in working directory, so I'm tring to set default value as working directory path.

My problem is simple. Arguments are entered all at once and do not come in order. So I cannot set parsed 1st argument as 2nd argument default value.

How do I make the second and third arguments default to values based on the first argument?

below code is not working.

import argparse
import os

if __name__=="__main__":
    parser = argparse.ArgumentParser( )
    parser.add_argument('--path', nargs='?', default='.\\', help='file location. Default is current location', type=str) 

    args = parser.parse_args() # this line is the beginning of problem. 
    default_PATH = args.path
    file1 = [x for x in os.listdir(default_PATH) if ".csv" in x][0]

    parser.add_argument('--file1', nargs=1, help='file1 name', type=str, default=file1) 

launch.json is below

            "args": ["--path",".\\","--file1", "arg3",], 
0

There are 0 best solutions below