0

I would like to add the directory path "C:\Program Files\Google\Chrome\Application" to the system environment variable using python script only.

As shown in the image below, I already have it added manually, but I would like to achieve this using python script.

I have tried using the code below but it does not work.

import os

new_path = "C:\Program Files\Google\Chrome\Application"
path = os.environ.get('PATH')
new_path_list = [new_path] + path.split(';')
os.environ['PATH'] = ';'.join(new_path_list)
  • works for me (I print the os environment var PATH and see the addition in the same script). What happens when you try? – JacobIRR Feb 15 '23 at 20:25
  • Your `new_path` contains backslashes - you need to either escape them (`"C:\\foo\\bar"`) or use a raw string (`r"C:\foo\bar"`) – slothrop Feb 15 '23 at 20:32
  • There is no image (which is a good thing; please [don’t post images of code, error messages, or other textual data.](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question/285557#285557)) – tripleee Feb 15 '23 at 20:43
  • @JacobIRR, nothing happens really. I go into the system environment variable to see if it has been added to the PATH and I see that it has not been added. – donaldekpe Feb 15 '23 at 21:45
  • @donaldekpe the variable will not persist with just python. You can add it and use it in the same program, but if you want it to persist, you should use BASH: https://stackoverflow.com/a/716046/4225229 – JacobIRR Feb 15 '23 at 21:52

0 Answers0