473,386 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

ending a string with a backslash

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.
Apr 30 '06 #1
6 3165
John Salerno wrote:
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?


see the first two items here:
http://www.ferg.org/projects/python_gotchas.html
May 1 '06 #2

"John Salerno" <jo******@NOSPAMgmail.com> wrote in message
news:44**********************@news.astraweb.com...
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

May 1 '06 #3
Edward Elliott wrote:
John Salerno wrote:
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?


see the first two items here:
http://www.ferg.org/projects/python_gotchas.html


#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?
May 1 '06 #4
John Salerno wrote:
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:
r'This\string\contains\backslashes' '\\'

'This\\string\\contains\\backslashes\\'

May 1 '06 #5
John Salerno schreef:
Edward Elliott wrote:
John Salerno wrote:
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?

see the first two items here:
http://www.ferg.org/projects/python_gotchas.html


#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:
os.path.join('foo', 'bar') 'foo\\bar' os.path.join('foo\\', 'bar')

'foo\\bar'

Greatly simplifies concatenating path components IMO.

--
If I have been able to see further, it was only because I stood
on the shoulders of giants. -- Isaac Newton

Roel Schroeven
May 1 '06 #6
Roel Schroeven wrote:
I think this is also the right time to mention os.path.join.


Yeah, I ended up using this and it looks a lot nicer too. :)
May 1 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Matt | last post by:
In test() method: var path="C:\test\hello.txt"; //returns -1 for path.lastIndexOf("\\"). why?? var pos=path.lastIndexOf("\\"); //return -1 But in showFile() method: We are able to get the...
1
by: Stephen | last post by:
Hi, I need to detect the presence of a backslash character in a string and escape that character using the replace function. I seem to be able to do this with any character except the backslash...
5
by: John Baro | last post by:
I have a richtextbox which I want the "literal" rtf of. richtextbox.rtf returns {\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033\\uc1 }\r\n\0 when i put this into a string I get...
3
by: aroraamit81 | last post by:
Hi, I am facing a problem in javascript. strdata is a variable of javascript and following is an assignment( hmmmmmmmmmmm a Huge one but unfortunately its not mine code, I am just trying to...
4
by: weirdstuff | last post by:
Hi. I have this simple code: =========================================== ->Database query here (.. some code) $row=mysql_fetch_array($res); (...)
5
by: shalini jain | last post by:
Hi, I want to know how to use split function to split the string based on backslash(\). Actually backslash is appearing at the end of the string. and i want to remove it. so if anyone could guide...
6
by: HMS Surprise | last post by:
The string s below has single and double qoutes in it. For testing I surrounded it with triple single quotes. I want to split off the portion before the first \, but my split that works with...
2
by: Tobiah | last post by:
>>"'" "'" "'" "\\'" "\\'" This is quite different than any other language that I am used to. Normally, a double backslash takes away the special meaning of the last backslash, and so you...
6
by: Russ P. | last post by:
I've always appreciated Python's lack of requirement for a semi-colon at the end of each line. I also appreciate its rules for automatic line continuation. If a statement ends with a "+", for...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.