0

I can run a script on the command line with an argument either as the bare "script.py arg1" or I can run it as "python script.py arg1". Both work until I use argparse for command line parameters.

The bare script does not recognize arg1:

Directories.py
usage: directories.py [-h] [-f FILE] directory
directories.py: error: the following arguments are required: directory

"python directories.py arg1" does work with argparse.

WHY?

Can I fix that bare script? Can I make the bare "directories.py arg1" work?

(I don't really understand why under Windows the bare script executes. I assume there is a python association.)

Here is the argparse part:

if __name__ == "__main__":
    
    parser = argparse.ArgumentParser(description="Get tree from single dir or dir list from file")  
    
    parser.add_argument("directory", type=str, help="Get single directory")
    
    # Adding argument
    parser.add_argument("-f", "--file", help = "Get directories from a file")
    
    # Read arguments from command line
    args = parser.parse_args()
        
    main(args)
9
  • 1
    It sounds like you might have an incorrect open command configured for .py files. To check, first open a command prompt (not Powershell) and run assoc .py. It should print .py= and something after the =. Run ftype whateverwasaftertheequalssign, substituting whatever was after the equals sign. Then show us everything those two commands printed. Commented Mar 5 at 5:20
  • This does look odd! C:\>assoc .py .py=Python.File C:\>ftype Python.File Python.File="C:\WINDOWS\py.exe" "%L" %* Commented Mar 5 at 6:19
  • C:\>py -V Python 3.7.15 C:\>python -V Python 3.6.4 Commented Mar 5 at 6:27
  • C:\>py --list unknown option --list usage: C:\Users\Bill\Anaconda3\python.exe [option] ... [-c cmd | -m mod | file | -] [arg] ... Try `python -h' for more information. Commented Mar 5 at 7:30
  • 1
    Add a print(sys.argv) diagnostic line at the beginning of your main. Commented Mar 5 at 12:41

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.