By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,908 Members | 1,850 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,908 IT Pros & Developers. It's quick & easy.

Construct raw strings?

P: n/a
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(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.

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

Thanks in advance,
Thomas

Sep 7 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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'
Note the lack of an "r" prefix and the \t sequence above.
The problem is that some of the parts being joined contains escape
characters 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.


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_images' instead.
--
Benji York
Sep 7 '05 #2

P: n/a
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
rk***@ucsd.edu

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

Sep 7 '05 #3

P: n/a
Benji York wrote:
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_images' instead.


Benji, you're confusing things: you probably meant r'd:\test_images' 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
Sep 8 '05 #4

P: n/a

"Thomas W" <th***********@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
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'


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

Sep 8 '05 #5

P: n/a
Peter Hansen wrote:
Benji York wrote:
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_images' instead.


Benji, you're confusing things: you probably meant r'd:\test_images'
in the above


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

Sep 8 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.