Going crazy because of \

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • PythonNewbie
    New Member
    • Nov 2006
    • 18

    Going crazy because of \

    Hello, I am having so many issues with \

    In a directory with a path like:
    "F:\somefolder\ 2007\somefolder \2007\somefolde r\*.txt files"

    I have been trying to make a path out of this to access the files with glob but \2007 keeps messing things up. It turns into \x807 when saved as string.

    Some of the solutions I can use to work around this are:
    1) I can work around it is by using os.path.join() and hardcoding all the folders names. However, I want to be able to just specify the directory at run time.

    2) I can use / instead of \ and then use split with /. This is reasonable but again it does not really work for me because I want to be able to just copy the directory path from the windows and paste it; not deal with changing all the slashes.

    Can someone please suggest something clever.
  • ilikepython
    Recognized Expert Contributor
    • Feb 2007
    • 844

    #2
    Originally posted by PythonNewbie
    Hello, I am having so many issues with \

    In a directory with a path like:
    "F:\somefolder\ 2007\somefolder \2007\somefolde r\*.txt files"

    I have been trying to make a path out of this to access the files with glob but \2007 keeps messing things up. It turns into \x807 when saved as string.

    Some of the solutions I can use to work around this are:
    1) I can work around it is by using os.path.join() and hardcoding all the folders names. However, I want to be able to just specify the directory at run time.

    2) I can use / instead of \ and then use split with /. This is reasonable but again it does not really work for me because I want to be able to just copy the directory path from the windows and paste it; not deal with changing all the slashes.

    Can someone please suggest something clever.
    Try making it a raw string. Add an "r" before the first quote. That will treat all backslashes as real backslashes. I think that might work.

    Comment

    • PythonNewbie
      New Member
      • Nov 2006
      • 18

      #3
      Aha, I was not aware of that functionality. I love this forum :) Thanksalot

      Comment

      • ilikepython
        Recognized Expert Contributor
        • Feb 2007
        • 844

        #4
        Originally posted by PythonNewbie
        Aha, I was not aware of that functionality. I love this forum :) Thanksalot
        Your welcome

        Comment

        Working...