1

I am trying to change the cwd but using os.path.join however when i send the string to os.chdir it throws an error because the string has extract backslashes added. See code below:

      cwd = os.getcwd()
       if cwd != os.path.join(os.environ["HOMEPATH"], "Desktop"):
     try:
        path = os.path.join(os.environ["HOMEPATH"], "Desktop") 

    print(path) #Added for debugging
    os.chdir(path)
except Exception as e:
    print(e)

The output is:

  • Print statement -> \Users\xxxx\Desktop
  • Error -> [WinError 2] The system cannot find the file specified: '\\Users\\xxxx\\Desktop'

What i have tried: - This question - This question - This question

using .replace and trying to convert to a raw string. At first I just passed the entire os.path.join into os.chdir.

ChrisAus
  • 39
  • 1
  • 2
  • What does `dir \Users\xxxx\Desktop` from `cmd` show? – metatoaster Sep 19 '18 at 02:51
  • On Windows, with what is supposed to be an absolute path (leading backslash) you need to specify the drive letter as well: `os.path.join(os.environ['HOMEDRIVE'], os.environ['HOMEPATH'], "Desktop")` – shmee Sep 19 '18 at 06:00

0 Answers0