9

I can't seem to change my directory in python:

import os

os.getcwd()

'C:\\Users\\Jon\\Folder\\IdbyGenotype'

os.chdir(r"C:\Users\Jon\Folder\IdbyGenotype\thisone")

os.getcwd()

'C:\\Users\\Jon\\Folder\\IdbyGenotype'

Am I missing something? What could be going wrong here?

Thanks

hylaeus
  • 245
  • 2
  • 3
  • 10
  • 1
    Does os.chdir raise an error? – Viktor Kerkez Aug 14 '13 at 18:53
  • This code works as is. Did you forget to add something to your post? –  Aug 14 '13 at 18:54
  • 1
    No it doesn't, which is very odd. It seems like no matter what directory I try to switch to, it doesn't change the default loading directory in my ide. I have tried shutting my computer down and closing out the program to no avail. – hylaeus Aug 14 '13 at 18:55
  • Wait, it doesn't change the *default* directory? Are you trying to have Python start by default in a different directory? If so, you'll need to change your IDE's configuration. `chdir` only changes the *current* working directory. – user2357112 Aug 14 '13 at 18:57
  • I had a hunch about that user2357112, thanks for the insight, I'll do an investigation and see what turns out. thanks all. – hylaeus Aug 14 '13 at 18:58
  • Could you edit your question to add the details that seem like they must be missing? I'm assuming if you run the code you posted in a single Python interactive shell session, `os.getcwd` will change after you call `os.chdir`. So I think you need to update what you asked to match what you meant to ask. As it stands your question makes it look like there's a bug in Python's `os.chdir` function. – bob Feb 11 '20 at 15:59

2 Answers2

2

Use forward-slash(/) in path whether you are using Windows or Linux

import os

os.getcwd()

'C:\\Users\\Jon\\Folder\\IdbyGenotype'

os.chdir("C:/Users/Jon/Folder/IdbyGenotype/thisone")

This worked in my case.

-2
 import os
 os.getcwd()
 'C:\\Program Files\\PYTHON'
 os.chdir('c:\\mytemp')
 os.getcwd()
 'c:\\mytemp'
 os.chdir(r'c:\')
 SyntaxError: EOL while scanning string literal
 os.chdir(r"c:\\")
 os.getcwd()
 'c:\\'

I have had inconsistent results with the use of r to communicate that the following is a raw string. As you can see when I use the r I get an error when I hit enter.

Thus have you tried to use escaped-backslashes in your os.chdir() command?

You are implying that you did not receive any error messages - this is strange because I get error messages when I try to chdir to a directory that I do not have rights to under my user name and when I try to chdir to a directory that does not exist.

PyNEwbie
  • 4,882
  • 4
  • 38
  • 86
  • I have tried both ways, to no avail. It's really odd. I haven't had a problem before. – hylaeus Aug 14 '13 at 20:15
  • Raw strings can't end in a backslash. It's a design decision that makes it easier to put quotes in raw strings. Raw strings are primarily designed for tools that want to do their own backslash processing, like `re`; in those applications, it doesn't make sense to end a raw string with a backslash, so this behavior was deemed more useful. – user2357112 Aug 15 '13 at 01:34