ending a string with a backslash

J

John Salerno

I have this:

subdomain = raw_input('Enter subdomain name: ')

path = r'C:\Documents and Settings\John Salerno\My Documents\My
Webs\1and1\johnjsalerno.com\' + subdomain

Obviously the single backslash at the end of 'path' will cause a
problem, and escaping it with a backslash seems to fix this problem, but
how does escaping work when I already have it as a raw string? When I
test it out and then print string, I get something like this:

C:\Documents and Settings\John Salerno\My Documents\My
Webs\1and1\johnjsalerno.com\\test

But I don't see how this is valid, since all the backslashes are single
(which is correct) except the last one. Somehow this still works when I
tried to create the new directory -- os.mkdir(path) -- but I wasn't sure
if this is the right way to go about it, or if there is some other,
better way to handle the final backslash.
 
T

Terry Reedy

John Salerno said:
I have this:

subdomain = raw_input('Enter subdomain name: ')

path = r'C:\Documents and Settings\John Salerno\My Documents\My
Webs\1and1\johnjsalerno.com\' + subdomain

For any direct use of paths within Python (to open a file, change
directory, etc), forward slashes work fine, avoiding backslash problems.

tjr
 
D

Dan Bishop

John said:
I have this:

subdomain = raw_input('Enter subdomain name: ')

path = r'C:\Documents and Settings\John Salerno\My Documents\My
Webs\1and1\johnjsalerno.com\' + subdomain

Obviously the single backslash at the end of 'path' will cause a
problem, and escaping it with a backslash seems to fix this problem, but
how does escaping work when I already have it as a raw string? When I
test it out and then print string, I get something like this:

C:\Documents and Settings\John Salerno\My Documents\My
Webs\1and1\johnjsalerno.com\\test

But I don't see how this is valid, since all the backslashes are single
(which is correct) except the last one. Somehow this still works when I
tried to create the new directory -- os.mkdir(path) -- but I wasn't sure
if this is the right way to go about it, or if there is some other,
better way to handle the final backslash.

As others have stated, you can use a forward slash. Alternatively, you
can write:
'This\\string\\contains\\backslashes\\'
 
R

Roel Schroeven

John Salerno schreef:
#2 was very helpful. But if I want to use forward slashes instead, can I
just replace the backslashes with them, or must I use the
os.path.normcase() function to do it?

You can just replace them: all internal Windows functions accept forward
slashed instead of backslashes in path names.

I think this is also the right time to mention os.path.join. It takes
any number of path components and joins them, taking care of placing
path delimiters between them. That means you could have written your
code as follows:

path = os.path.join(r'C:\Documents and Settings\John Salerno\My
Documents\My Webs\1and1\johnjsalerno.com', subdomain)

It also handles the case where there is a trailing backslash:
'foo\\bar'

Greatly simplifies concatenating path components IMO.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top