9

I'm trying to change the current directory from C: to Y: I tried:

import os
os.chdir('Y:')

but I keep getting an error saying that it can't locate the drive. Essentially I'm looking for the equivalent of the

cd /d

command in cmd.

heltonbiker
  • 26,657
  • 28
  • 137
  • 252
aensm
  • 3,325
  • 9
  • 34
  • 44
  • 4
    You realize that "change the current directory from C: to Y:" doesn't actually mean anything in Windows? There's a current default drive, and a separate current directory for each drive. So, you can change the current drive to Y:, or you can change the current drive to Y: and change the directory on that to \, but those are different operations. (At the C API level it's _chdrive(25) vs. _chdir("Y:\\"), and it's probably similar in Python, except that chdrive probably doesn't exist in os and you have to go to msvcrt, or even ctypes the actual MSVCRT DLL.) – abarnert Jun 15 '12 at 21:21
  • I realized my connection to Y: had been disrupted somehow, but once I sorted that out chdir('Y:') worked fine. Thanks for the help in any case. – aensm Jun 18 '12 at 18:54

3 Answers3

12

Are you sure Y: really is a valid drive letter?

Try os.chdir('C:') and make sure that works. (It works for me.)

Jon-Eric
  • 16,977
  • 9
  • 65
  • 97
3

If this is a mapped network drive, your best bet is to use the UNC path instead of the mapped path. Also, try to use a raw r string modifier when using paths under windows, if you're not using os.path.join.

import os
print os.getcwd()
os.chdir(r'\\server\path') 
print os.getcwd()
PenguinCoder
  • 4,335
  • 1
  • 26
  • 37
-1

If you are doing (Drive:path\to\folder) try switching the slashes to (Drive:path/to/folder)

JawsChamp
  • 1
  • 2