Home > Back-end >  Subprocess $Env:Path python: The filename, directory name, or volume label syntax is incorrect
Subprocess $Env:Path python: The filename, directory name, or volume label syntax is incorrect

Time:05-10

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
>>> subprocess.run("$Env:Path", shell=True)
The filename, directory name, or volume label syntax is incorrect.

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

CodePudding user response:

Python's subprocess on Windows does not call Powershell by default but CMD shell. So try the analogous CMD command:

subprocess.run("echo %Path%", shell=True)

But if you need Powershell, explicitly call it with your command:

subprocess.run("powershell $Env:Path", shell=True)
  • Related