Subprocess $Env:Path python: The filename, directory name, or volume label syntax is incorrect

I am trying to change the windows environment variables, but I am having trouble doing so.

Before I tried to use os.environ() I tried out using powershell commands and adding a string to $Env:Path which worked, but removing it with:

$env:Path = ($env:Path.Split(';') | Where-Object -FilterScript {$_ -ne $Remove}) -join ';'

however didn't seem to remove it being my path I want to add ("FFmpeg:C:\Users\user\AppData\") and adding it with += C:/Users/etc.. didn't see, the way to go.

Another way I tried to add vars through the Powershell commands was using SetEnviormentVariable and it seemed to work fine but once I restarted my PC the entry I made with it was gone.

Sadly though all in the end all my powershell commands didn't work with subprocess. Whatever command it was I was using here I got:

PS C:\Users\Me123> python
>>> import subprocess
>>>"$Env:Path", shell=True)
The filename, directory name, or volume label syntax is incorrect.

CompletedProcess(args='$Env:Path', returncode=1)

1 answer

  • answered 2022-05-08 04:30 Parfait

    Python's subprocess on Windows does not call Powershell by default but CMD shell. So try the analogous CMD command:"echo %Path%", shell=True)

    But if you need Powershell, explicitly call it with your command:"powershell $Env:Path", shell=True)

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum