0

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?

Community
  • 1
  • 1
Katya Willard
  • 2,152
  • 4
  • 22
  • 43
  • Do you mean to put an r before my string? I have also tried that and it does not work. – Katya Willard Jul 28 '15 at 21:27
  • You have to double each backslash. The leading double-backslash becomes 4 backslashes, like so "\\\\CNYC19P20003d\\khandler$\\ .....". Or, a raw string, like R"\\CNYC19P20003d\khandler$\ ....." – joeking Jul 28 '15 at 21:28
  • Please note that the r"foo" syntax doesn't allow you to have a backslash as the last character. I don't know why, seems like a bug to me. R"\This\Is\Broken\" – joeking Jul 28 '15 at 21:29
  • @joeking, I tried to double each backslash, no luck. I tried to create a raw string both with uppercase R and with lowercase r, no luck. Also, I have no trailing backslash. – Katya Willard Jul 28 '15 at 21:31
  • Found this on [Google](https://support.microsoft.com/en-us/kb/156276). – Dustin Anderson Jul 28 '15 at 21:44
  • So you want to set the _cwd_ to a path from another?(or is it local) computer(`CNYC19P20003D`), in `khandler$` (I assume `khandler` is an user on that computer), and the rest (which is not important)?. My question is: what user and on what machine does your program run? I don't get the `M:` drive...What is that? At first I thought it was a mapped network drive, but (some of) the examples posted blew that away. – CristiFati Jul 28 '15 at 22:15
  • @CristiFati, I am assuming that my program is running on C:\Windows, as when I ask it to print my current working directory, that is what it returns. The M: drive is another drive on my computer, and the M: drive is where I would like my working directory to be. – Katya Willard Jul 29 '15 at 11:54

0 Answers0