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)
5
  • works for me (I print the os environment var PATH and see the addition in the same script). What happens when you try? Commented Feb 15, 2023 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") Commented Feb 15, 2023 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.) Commented Feb 15, 2023 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. Commented Feb 15, 2023 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: stackoverflow.com/a/716046/4225229 Commented Feb 15, 2023 at 21:52

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.