September 10th, 2004, 09:54 AM
how do I stop this string behavior?
Python 2.3.3 (#1, Aug 15 2004, 19:14:24)
[GCC 2.95.3 20010315 (release)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> dirs = "\a"
I'm doing lots of code with directories and it seems to turn \SomeCharHere code into other text most of the time. I think it is interperting it to be unicode, but i'm not really sure. Either way I would like it to not do this because that is not what I want. Right now I have part of a script that deletes directories so it is having problems with telling the rm command the right filename to delete.
September 10th, 2004, 10:14 AM
You need to use RAW strings:
dirs = r"\a"
dirs = R"\a"
By the way - '\a' will be interpreted as a control character not as Unicode.
Python 2.3 reference
and search for "\a"
September 10th, 2004, 11:02 AM
It would be even simpler if you used the character "/" (which is the normal character for directories on Linux) instead of "\". Normally, this should also work under Windows.
Note that there is a python function in the os module that returns the appropriate directory character depending on the operating system your script is running on.
September 10th, 2004, 01:21 PM
/'s did the job
Thanks for the reference site too I like it.
September 10th, 2004, 11:29 PM
Another way to do this: you can also manually escape the backslash with another backslash, which is why its not uncommon to see paths under windows that look like:
Though IMO its always best to use '/' in paths; since in my experience Python doesn't force you to use the same characters as the OS.
Last edited by netytan; September 10th, 2004 at 11:32 PM.
September 11th, 2004, 01:03 PM
Or just have a list of each element in the path and do:
And that's nice and platform-independent (since os.path.sep changes on whichever platform you are using it on)
os.path.sep.join(['foo', 'bar', 'baz'])