[Python] How to open file using argparse?

Discussão em 'Python' iniciado por Stack, Fevereiro 16, 2025.

  1. Stack

    Stack Membro Participativo

    I want to open file for reading using argparse. In cmd it must look like: my_program.py /filepath

    That's my try:

    parser = argparse.ArgumentParser()
    parser.add_argument('file', type = file)
    args = parser.parse_args()


    This gives me [edit: added later from comment by OP]:

    parser.add_argument('file', type = file) NameError: name 'file' is not defined

    Continue reading...

Compartilhe esta Página