#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    3
    Rep Power
    0

    how do I stop this string behavior?


    # python
    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"
    >>> dirs
    '\x07'

    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.
  2. #2
  3. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    radfaraf,

    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.

    Check out:
    Python 2.3 reference
    and search for "\a"

    grim
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    France
    Posts
    53
    Rep Power
    15
    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.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Posts
    3
    Rep Power
    0
    /'s did the job
    Thanks for the reference site too I like it.
  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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:

    path = 'C:\\Python23\\lib\\string.py'
    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.

    Mark.
    Last edited by netytan; September 10th, 2004 at 11:32 PM.
    programming language development: www.netytan.com Hula

  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    Houston, TX
    Posts
    383
    Rep Power
    13
    Or just have a list of each element in the path and do:

    Code:
    os.path.sep.join(['foo', 'bar', 'baz'])
    And that's nice and platform-independent (since os.path.sep changes on whichever platform you are using it on)
    Debian - because life's too short for worrying.
    Best. (Python.) IRC bot. ever.

IMN logo majestic logo threadwatch logo seochat tools logo