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

Filepath string manipulation help

P: n/a
Hi,

I'm trying to use some string manipulation from a file's path.

filepath='c:\documents\web\zope\file.ext'

I need to extract everthing after the last '\' and save it.
I've looked around and have tried the sub, search, match commands, but
I'm guessing '\' is set aside for switches. I need to know how to
search for the '\' in a string. I'm guessing it has something to do
with ASCII char codes.

Thanks in advance for any help,
mjakowlew

Nov 2 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
import os
print os.path.basename(filepath)

Nov 2 '05 #2

P: n/a
"mjakowlew"wrote:
filepath='c:\documents\web\zope\file.ext'

I need to extract everthing after the last '\' and save it.


that string doesn't contain what you think it does:
filepath='c:\documents\web\zope\file.ext'
filepath 'c:\\documents\\web\\zope\x0cile.ext' print filepath c:\documents\web\zope?ile.ext

if you fix that, you can use os.path.basename() to strip off the last
part:
filepath=r'c:\documents\web\zope\file.ext'
filepath 'c:\\documents\\web\\zope\\file.ext' print filepath c:\documents\web\zope\file.ext
import os
os.path.basename(filepath)

'file.ext'

for more info on Python's string literal syntax, see section 3.1.2 in the
Python Tutorial, and this page:

http://docs.python.org/ref/strings.html

</F>

Nov 2 '05 #3

P: n/a
Thanks guys. The os.path method works, but this is for a script for a
website upload program on a zope webserver. For some reason even admin
access won't let me run the script through the server.

The main issue with my script is that Firefox has no problem with the
program the way it is, but IE somehow uses a different filename format.

ex:/

In IE
filepath='c:\documents\web\zope\file.ext'

In Firefox
filepath='file.ext'

So when IE goes to upload the file, it gets an error for illegal
characters because of the "\"'s and ":" Is there another way to do this
(extract just the filename) using something else other than the "os"
that won't be blocked by the webserver

Nov 3 '05 #4

P: n/a
Hi mjakowlew,
to get file basename in Linux I use simply:
filepath.split('/')[-1]
But in Windows, being the dir separator '\',
you get into trouble if the dir or file name begins
with one of the "escape sequences":
\a ASCII Bell (BEL) \x07
\b ASCII Backspace (BS) \x08
\f ASCII Formfeed (FF) \x0c
\n ASCII Linefeed (LF)
\r ASCII Carriage Return (CR)
\t ASCII Horizontal Tab (TAB)
\v ASCII Vertical Tab (VT) \x0b
(from the ref. suggested by Fredrik Lund).

To solve the problem you must use "raw strings",
as suggested by the aforementioned expert.

So your filepath ('c:\documents\web\zope\file.ext')
becomes r'c:\documents\web\zope\file.ext' which
protects the '\' by escaping it ('\\').

With such a trick you can obtain the file basename with:
filepath.split('\\')[-1].
Bye.

Nov 3 '05 #5

P: n/a
I got the "IE Fix" working, here's the code:

____________________________
path = r'c:\here\there\files\file.ext'

i=len(path)
j=0
size=len(path)

while i:
i=i-1
if path[i]== '\\':
j=i+1
break

filename = path[j:size]
print "FILENAME: %s" %(filename)
_______________________________

Most importantly this works on my Zope webserver.

Thanks again,
mjakowlew

Nov 3 '05 #6

P: n/a
mjakowlew wrote:
I got the "IE Fix" working, here's the code:

____________________________
path = r'c:\here\there\files\file.ext'

i=len(path)
j=0
size=len(path)

while i:
i=i-1
if path[i]== '\\':
j=i+1
break

filename = path[j:size]
print "FILENAME: %s" %(filename)
_______________________________

Most importantly this works on my Zope webserver.

Thanks again,
mjakowlew

Is there some reason for preferring this to the fully portable

import os
print os.path.basename(filepath)

that Robert Dowell suggested in immediate response to your post?

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Nov 3 '05 #7

P: n/a
Steve,

the os commands don't run through zope, it denies access to them to be
run at all through the webserver. So in turn, I had to use a work
around to fix the IE problem. Also qwwee's suggestion to use:

filepath.split('\\')[-1]

works well too. Zope is very finicky about running specific commands,
I'm sure this is due to security issues of running os commands through
a website/webserver.

Nov 4 '05 #8

P: n/a
mjakowlew wrote:
Steve,

the os commands don't run through zope, it denies access to them to be
run at all through the webserver. So in turn, I had to use a work
around to fix the IE problem. Also qwwee's suggestion to use:

filepath.split('\\')[-1]

works well too. Zope is very finicky about running specific commands,
I'm sure this is due to security issues of running os commands through
a website/webserver.

That's a good enough reason. You are excused. :-)

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Nov 4 '05 #9

P: n/a
I just assumed he had heard of me and knew better than to take my
advice. :-)

Nov 4 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.