trouble using "\" as a string

O

OriginalBrownster

Hi there...

I'm still pretty new to turbogears. but i have gotten pretty familiar
with it

i'm just trying to clear something up, i'm having a difficult time
using \ when declaring a string expression

such as tempname="\"..it says that the line is single qouted.

i want this because using python I am pulling in filenames from a
mac..thus they are "/" in the pathways..and i want to .split it at the
"/" to obtain the filename at the end...but its proving diffucult with
this obstacle in the way.

Why is this happening??
 
O

OKB (not okblacke)

OriginalBrownster wrote:
[I un-bottom-posted this]
that stores tempname as "\\"

..

I don't think so. I think you just think it's storing it as that
because if you enter it at the interactive prompt it shows '\\'. But
try len(tempname) and see if it's 1.

--
--OKB (not okblacke)
Brendan Barnwell
"Do not follow where the path may lead. Go, instead, where there is
no path, and leave a trail."
--author unknown
 
D

Dan Bishop

OriginalBrownster said:
Hi there...

I'm still pretty new to turbogears. but i have gotten pretty familiar
with it

i'm just trying to clear something up, i'm having a difficult time
using \ when declaring a string expression

such as tempname="\"..it says that the line is single qouted.

i want this because using python I am pulling in filenames from a
mac..thus they are "/" in the pathways..and i want to .split it at the
"/" to obtain the filename at the end...but its proving diffucult with
this obstacle in the way.

Why is this happening??

In Python (and many other programming languages), the backslash is the
"escape character". That is, special characters are indicated by
sequences that start with a backslash.
Line 1
Line 2"It's necessary to escape a quote that's within the same type of
quote."

An escape sequence is always considered a single character. (As
mentioned earlier, you can verify this with the len function.) Thus,
your "\" gets parsed as:

" = opening quote of string
\" = double quotation mark (escaped)
end of line = error, because no closing quote was found

If you want a backslash, you have to escape *it* with a backslash: "\\"
 
J

Justin Azoff

OriginalBrownster said:
i want this because using python I am pulling in filenames from a
mac..thus they are "/" in the pathways..and i want to .split it at the
"/" to obtain the filename at the end...but its proving diffucult with
this obstacle in the way.

sounds like you want
import posixpath
posixpath.basename(path)

assuming you are on a windows box,otherwise the normal os.path.basename
will do it.
 
A

Ant

such as tempname="\"..it says that the line is single qouted.

The others have addressed the escape issue I think. However it looks
like you want the funtionality of the os.path module. For example:
('c:\\python24\\scripts', 'test.py')
 

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
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top