Construct raw strings?

Robert Kern rkern at ucsd.edu
Wed Sep 7 18:33:25 EDT 2005


Thomas W wrote:
> 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'

path_to_scan = r'd:\test_images'

> This is used in a larger context and joined like
> 
> real_path_after_scanning = os.path.join(path_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 :
> 
>>>>f = r'd:\test_images\something\foo\bar\test.jpg'
> 
> 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.

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 at ucsd.edu

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




More information about the Python-list mailing list