[Python] How to run CMD commands e.g. whoami and dir with python subprocess.run on Windows

Discussão em 'Python' iniciado por Stack, Abril 15, 2025.

  1. Stack

    Stack Membro Participativo

    I have this code: which is just a snippet of a larger application


    import subprocess

    print(subprocess.run(["dir"],text=True,capture_output=True))



    Which is just a snippet of a larger application.

    Which should output something around the lines of:


    04/09/2025 01:46 PM <DIR> random
    04/09/2025 01:46 PM <DIR> something else
    04/15/2025 11:56 AM <DIR> files



    Instead I get this yucky error which I have shortened because it is very long:


    FileNotFoundError: [WinError 2] The system cannot find the file specified



    Thank you in advance.

    Continue reading...

Compartilhe esta Página