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
.