I learned today that in python on windows, doing os.chdir('f:\\')
is very different than doing os.chdir('f:')
. The command with the trailing backslash actually changes the current directory. Without the backslash, the command is like typing f:
in the command prompt, and changes the current directory to the last current directory on the f
drive.
Here's an example:
import time
import os
if __name__ == '__main__':
os.chdir('f:\\directory')
print 'cwd is {}'.format(os.getcwd())
os.chdir('d:\\directory')
print 'cwd is {}'.format(os.getcwd())
os.chdir('f:')
print 'cwd is {}'.format(os.getcwd())
os.chdir('f:\\')
print 'cwd is {}'.format(os.getcwd())
prints
cwd is f:\directory
cwd is d:\directory
cwd is f:\directory
cwd is f:\
I can understand why this difference is useful, but couldn't find anything documenting this behavior. I'd gotten used to leaving trailing slashes off my directories as I usually use here strings...
Is this behavior documented anywhere? Can I rely on it not changing?