Construct raw strings?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Thomas W

    #1

    Construct raw strings?

    I got a stupid problem; on my WinXP-box I want to scan the filesystem
    and enter a path to scan like this :

    path_to_scan = 'd:\test_images '

    This is used in a larger context and joined like

    real_path_after _scanning = os.path.join(pa th_to_scan, somepart, 'foo',
    'bar', filename)

    Using os.path.exists( real_path_after _scanning) returns false. The
    problem is that some of the parts being joined contains escape
    characters, like \. If I take the seperate parts and join them using
    the interpreter, like :[color=blue][color=green][color=darkred]
    >>> f = r'd:\test_image s\something\foo \bar\test.jpg'[/color][/color][/color]

    it works ok and os.path.exists( f) returns True, but I cannot the that
    r' in front using the os.path.join-method in my code.

    I don't know if this makes any sense at all, but I'm lost. Damn those
    stupid windows-paths !!

    Thanks in advance,
    Thomas

  • Benji York

    #2
    Re: Construct raw strings?

    Thomas W wrote:[color=blue]
    > I got a stupid problem; on my WinXP-box I want to scan the filesystem
    > and enter a path to scan like this :
    >
    > path_to_scan = 'd:\test_images '[/color]

    Note the lack of an "r" prefix and the \t sequence above.
    [color=blue]
    > The problem is that some of the parts being joined contains escape
    > characters[/color]
    [color=blue]
    > If I take the seperate parts and join them using the interpreter,
    > like :
    >[color=green][color=darkred]
    >>>> f = r'd:\test_image s\something\foo \bar\test.jpg'[/color][/color]
    >
    > it works ok and os.path.exists( f) returns True, but I cannot the that
    > r' in front using the os.path.join-method in my code.[/color]

    It's not join that's getting you, it's the non-raw string representation
    in path_to_scan. Use either 'd:\test_images ' or 'd:\\test_image s' instead.
    --
    Benji York


    Comment

    • Robert Kern

      #3
      Re: Construct raw strings?

      Thomas W wrote:[color=blue]
      > I got a stupid problem; on my WinXP-box I want to scan the filesystem
      > and enter a path to scan like this :
      >
      > path_to_scan = 'd:\test_images '[/color]

      path_to_scan = r'd:\test_image s'
      [color=blue]
      > This is used in a larger context and joined like
      >
      > real_path_after _scanning = os.path.join(pa th_to_scan, somepart, 'foo',
      > 'bar', filename)
      >
      > Using os.path.exists( real_path_after _scanning) returns false. The
      > problem is that some of the parts being joined contains escape
      > characters, like \. If I take the seperate parts and join them using
      > the interpreter, like :
      >[color=green][color=darkred]
      >>>>f = r'd:\test_image s\something\foo \bar\test.jpg'[/color][/color]
      >
      > it works ok and os.path.exists( f) returns True, but I cannot the that
      > r' in front using the os.path.join-method in my code.[/color]

      There is no such thing as a "raw string." There are "raw string
      literals" which, when evaluated as Python source, are interpreted with
      slightly different rules than regular "string literals." After the
      literal has been interpreted to yield a string object, there is no
      difference between the two; they're both just string objects.

      If you can, please post a small but complete example script that causes
      errors (and the actual output of the error itself).

      --
      Robert Kern
      rkern@ucsd.edu

      "In the fields of hell where the grass grows high
      Are the graves of dreams allowed to die."
      -- Richard Harter

      Comment

      • Peter Hansen

        #4
        Re: Construct raw strings?

        Benji York wrote:[color=blue]
        > It's not join that's getting you, it's the non-raw string representation
        > in path_to_scan. Use either 'd:\test_images ' or 'd:\\test_image s' instead.[/color]

        Benji, you're confusing things: you probably meant r'd:\test_image s' in
        the above, but in any case I think Robert Kern's on the right track
        checking whether this is really about string literals...

        -Peter

        Comment

        • Terry Reedy

          #5
          Re: Construct raw strings?


          "Thomas W" <thomas.weholt@ gmail.com> wrote in message
          news:1126130881 .555483.296200@ g47g2000cwa.goo glegroups.com.. .[color=blue]
          >I got a stupid problem; on my WinXP-box I want to scan the filesystem
          > and enter a path to scan like this :
          >
          > path_to_scan = 'd:\test_images '[/color]

          I believe you can always use / instead of \ for Win filenames from Python.
          Avoids the \ problem. I think only the shell that uses / for options has a
          problem with / in filenames, but Python calls directly to C.

          Terry J. Reedy



          Comment

          • Benji York

            #6
            Re: Construct raw strings?

            Peter Hansen wrote:[color=blue]
            > Benji York wrote:
            >[color=green]
            >> It's not join that's getting you, it's the non-raw string
            >> representation in path_to_scan. Use either 'd:\test_images ' or
            >> 'd:\\test_image s' instead.[/color]
            >
            > Benji, you're confusing things: you probably meant r'd:\test_image s'
            > in the above[/color]

            Doh! I did indeed. Thanks for the backup.
            --
            Benji York



            Comment

            Working...