Filepath string manipulation help

M

mjakowlew

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
 
F

Fredrik Lundh

"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:
c:\documents\web\zope?ile.ext

if you fix that, you can use os.path.basename() to strip off the last
part:
c:\documents\web\zope\file.ext
'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>
 
M

mjakowlew

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
 
Q

qwweeeit

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.
 
M

mjakowlew

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== '\\':
j=i+1
break

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

Most importantly this works on my Zope webserver.

Thanks again,
mjakowlew
 
S

Steve Holden

mjakowlew said:
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== '\\':
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
 
M

mjakowlew

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.
 
S

Steve Holden

mjakowlew said:
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
 

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

Forum statistics

Threads
474,265
Messages
2,571,071
Members
48,771
Latest member
ElysaD

Latest Threads

Top