Reputation: 778
I am getting an "Illegal characters in path" error when doing chdir commands in Iron Python. This is happening in run time with my code, but even in the Iron Python console it has this issue. I'm using the nt module because in code the os module does not work (appears to be a known issue).
Doing a little bit of playing around it turns out the "illegal characters" is actually the word bin. Below is the text from the console that shows me getting the error only when i navigate to the bin directory.
Here is the example
>>> nt.chdir('c:\Users\xxxxx\Documents\Visual Studio 2010\Projects\xxx')
>>> nt.chdir('c:\Users\xxxxx\Documents\Visual Studio 2010\Projects\xxx\Directory')
>>> nt.chdir('c:\Users\xxxxx\Documents\Visual Studio 2010\Projects\xxx\Directory\bin')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: Illegal characters in path.
Whats worse is I'll navigate to a totally different directory (that doesn't even have a bin directory) and try to navigate to a subdirectory "bin" and i'll still get that error!
Any Ideas?
Upvotes: 9
Views: 10704
Reputation: 177
Windows and most other operating systems will recognize the forward slashes. So, instead of the backslash, double-backslash, or r' ' (more on the string literals here) you can just use forward slashes and you are good to go. The answer here is also well detailed.
UPDATE: PS: Use backslashes and string literals with caution. Always check for your specific case. There is a good comment on that in this answer
Upvotes: 1
Reputation: 1121166
The \
path separator is also a python escape character. Double them, or better yet, use r''
raw python literals instead:
r'c:\Users\xxxxx\Documents\Visual Studio 2010\Projects\xxx'
'c:\\Users\\xxxxx\\Documents\\Visual Studio 2010\\Projects\\xxx'
For example, \n
is a newline character, and \t
is interpreted as a TAB. In your specific case, \b
is interpreted as a backspace.
Upvotes: 16
Reputation: 184071
\
is an escape character in Python strings. \b
is backspace, which is why it barfs on \bin
: you are not specifying the directory Directory\bin
, you are specifying the directory Directory<backspace>in
, which is not a legal path and even if it were, does not exist.
You can write the string by doubling the backslashes or by using the r
indicator as suggested by Martijn. A third alternative is to simply use forward slashes as in every other civilized operating system. Windows is perfectly happy to use these.
Upvotes: 6