[Python] Powershell and CMD combining command-line filepath arguments to Python

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

  1. Stack

    Stack Membro Participativo

    I was making user-entered variable configurable via command line parameters & ran into this weird behaviour:

    PS D:> python -c "import sys; print(sys.argv)" -imgs ".\Test V4\Rilsa\" -nl 34
    ['-c', '-imgs', '.\\Test V4\\Rilsa" -nl 34']

    PS D:> python -c "import sys; print(sys.argv)" -imgs ".\TestV4\Rilsa\" -nl 34
    ['-c', '-imgs', '.\\TestV4\\Rilsa\\', '-nl', '34']


    If the name of my folder is Test V4 with a space character, then all following parameters end up in the same argument element '.\\Test V4\\Rilsa" -nl 34'. There is also a trailing " quote after the directory name. I tried this again in CMD, thinking it was a Powershell quirk & experienced the same behaviour.

    What's going on here? I'm assuming it has something to do with backslashes in Powershell -- though it's the default in Windows for directory paths -- but why do I get diverging behaviour depending on space characters & what's a good way to handle this assuming Windows paths are auto-completed into this form by the shell (i.e. trailing \)?

    Continue reading...

Compartilhe esta Página