tags:

views:

89

answers:

5

I have problem with refering to special symbol in string:

I have: path='C:\dir\dir1\dir2\filename.doc'

and I want filename.

When I try: filename=path[path.rfind("\"):-4]

then interpreter says it's an error line right from "\" since is treated as a comment.

+1  A: 

Escape it:

filename=path[path.rfind("\\"):-4]

or better yet, use basename - part of Python's os.path library:

from os.path import basename
basename(path)

From the manual for basename:

Return the base name of pathname path. This is the second half of the pair returned by split(path). Note that the result of this function is different from the Unix basename program; where basename for '/foo/bar/' returns 'bar', the basename() function returns an empty string ('').

Dominic Rodger
+2  A: 

Either escape it as "\\" or use raw strings like so: r"\".

Plumenator
You can't end raw strings with `\ `, so `r"\"` doesn't work
gnibbler
@Gnibbler: `path = r'C:\dir\dir1\dir2\filename.doc'` works. What did you have in mind?
Manoj Govindan
@Manoj: @Gnibbler is right, `r"\"` does not work (and neither does `r'C:\dir\dir1\dir2\'`), because even in raw strings, `\'`/`\"` still escapes (it has to)
delnan
@delnan: understood.
Manoj Govindan
Wow, I didn't know that.
Plumenator
A: 

You can type double the \: "\".

However to split the filename out of Windows filename, use partition as os.path.split only works with /:

>>> path=r'C:\dir\dir1\dir2\filename.doc'
>>> print path.rpartition('\\')[-1]
filename.doc
Tony Veijalainen
You can't end raw strings with `\ `, so `r"\"` doesn't work
gnibbler
Noticed it while playing with the IDLE and edited 10 minutes before your comment. Same time as I put in the rpartition code. Maybe \ is not working for me for os.path.split as I am now running Linux. I think it and basename should work.
Tony Veijalainen
Edited 10 minutes _after_ my comment :)
gnibbler
Damn this screen resolution. Mixed it up. But without purpose. I read your comment only after correcting my post. I only learned it through my testing, so thanks for sharing your knowledge (or is it painful memories ;) )
Tony Veijalainen
+11  A: 

You can use "\\", technically it would be better to use os.path.sep if you insist on using backslashes. But better yet, use / in your paths, it works fine on Windows

Python has builtin functions to manipulate paths. Note that you need to double the backslashes if you still prefer them to forwardslashes

>>> import os
>>> path='C:\\dir\\dir1\\dir2\\filename.doc'
>>> os.path.splitext(os.path.basename(path))
('filename', '.doc')

and using forwardslashes

>>> path='C:/dir/dir1/dir2/filename.doc'
>>> os.path.splitext(os.path.basename(path))
('filename', '.doc')
gnibbler
+1 for pointing to the already-working and also portable standard implementation.
delnan
+1 for what the same thing @delnan said
Bob Fincheimer
+1: Very nice answer. Forgot this, though: http://docs.python.org/library/os.path.html
S.Lott
@S.Lott, good idea. linked
gnibbler
thank's a lot. it's working fine now:)
Lewy
A: 

There is something about this in the FAQ: Why can’t raw strings (r-strings) end with a backslash?

If you’re trying to build Windows pathnames, note that all Windows system calls accept forward slashes too:

f = open("/mydir/file.txt")  # works fine!

If you’re trying to build a pathname for a DOS command, try e.g. one of

dir = r"\this\is\my\dos\dir" "\\"
dir = r"\this\is\my\dos\dir\ "[:-1] 
dir = "\\this\\is\\my\\dos\\dir"
leoluk