I would like to change my working directory in Python. I am on Windows programming in the Sublime IDE. I am using Python 2.7.
The path I would like, harvested directly from my windows explorer, is: \\CNYC19P20003D\khandler$\Documents\ShortSqueeze
I am aware of the function os.chdir("path")
.
However, it seems I am having issues with the fact that Python uses '\' as an escape key, and Windows uses '\' to move a folder level down. Here is what I have so far:
import os
if __name__ == '__main__':
path = os.getcwd()
print "Current working directory:"
print path
os.chdir("M:\CNYC19P20003D\khandler$\Documents\ShortSqueeze")
file_object = open("rawData.txt", 'r')
The output of this code is the following:
'\\CNYC19P20003D\khandler$\Documents\ShortSqueeze'
CMD.EXE was started with the above path as the current directory.
UNC paths are not supported. Defaulting to Windows directory.
Current working directory:
C:\Windows
Traceback (most recent call last):
File "\\CNYC19P20003D\khandler$\Documents\ShortSqueeze\practice.py", line 9, in <module>
file_object = open("rawData.txt", 'r')
IOError: [Errno 2] No such file or directory: 'rawData.txt'
Here is what I have tried so far:
os.chdir("M:\\CNYC19P20003D\khandler$\Documents\ShortSqueeze")
os.chdir("\\CNYC19P20003D\khandler$\Documents\ShortSqueeze")
os.chdir("//CNYC19P20003D/khandler$/Documents/ShortSqueeze")
os.chdir("\\CNYC19P20003D\\khandler$\\Documents\\ShortSqueeze")
os.chdir(r"M:\CNYC19P20003D\khandler$\Documents\ShortSqueeze")
Along with many other variations on the same idea.
I spent some time looking at this stackoverflow article: Python os.chdir is modifying the passed directory name. However, it seems his issue was that part of his path had a specific escape code in it, \201. Since I don't have anything like \n or \t, I figured my issue was different.
How can I change my current working directory to my desired path? Is it possible that I have copied my path wrong? Is this an issue with backslashes?