Why does Python (IronPython) report "Illegal characters in path" when the word bin is used?
Asked Answered
C

3

9

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?

Cableway answered 11/9, 2012 at 13:57 Comment(0)
S
16

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.

Suzan answered 11/9, 2012 at 13:59 Comment(1)
DOH. I knew that... I guess it just so happens that it was ok escaping all of those particular characters except b..... thanks!Cableway
T
6

\ 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.

Trigger answered 11/9, 2012 at 14:4 Comment(4)
+1 for teaching me that windows (which I happily can avoid most of the time) nowadays accepts '/' path separators like a normal OS.Suzan
I never use the Windows pathing myself. I'm actually not generating the paths myself but using the .Net Path class to create "platform specfic paths". So obviously it will act differently on Windows than Linux (my preferred). Obviously you get issues like this where I now have to baby step it through, as you clearly point out, Windows isn't just knuckle dragging, but just an awful os in general.Cableway
Actually it turns out that Python (the nt module) is the culprit and is giving me paths WITHOUT double slashing them..Cableway
@Mike: No, that's not the problem. The double-slashing (or other workaround) is only required in string literals. It gets a single backslash into the string, which is correct.Trigger
A
1

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

Anderegg answered 6/8, 2013 at 19:20 Comment(3)
This is actually not true on Windows in all circumstances. There are numerous applications, functions within windows that require \ for pathing. Just open a command prompt, type "copy ./directory/file.txt ./locationdir" and you'll see what i mean.Cableway
@Mike: You definitely are right. I definitely agree that this does not work 100% of the time even in Windows. Crazy how nothing is ever completely universal when it comes to os. stuff. I also read from the answer I linked to in my reply that: "So raw strings are not 100% raw, there is still some rudimentary backslash-processing."Anderegg
The reply above with "every other civilized operating system." I think applies. Windows has a nonsense design that's neither good for users, business people, nor developers. I try to do as much development on Linux as I can to avoid this kind of stuff. I need to keep a tally of the locations where / is not accepted in windows, because another guy and myself come across this one again and again and again.Cableway

© 2022 - 2024 — McMap. All rights reserved.