473,320 Members | 1,969 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,320 software developers and data experts.

Filepath string manipulation help

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
9 5646
import os
print os.path.basename(filepath)

Nov 2 '05 #2
"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
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
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
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
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
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
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
I just assumed he had heard of me and knew better than to take my
advice. :-)

Nov 4 '05 #10

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

Similar topics

32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
29
by: zoro | last post by:
Hi, I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party string handling library that includes some very useful string functions, in particular I'm interested in BEFORE (return...
5
by: starritt | last post by:
I have to create a view where the filename is seperated from the path to the file. Examples of the data include: m:\images\big\myimg.jpg m:\images\medium\myimg.jpg...
4
by: WaterWalk | last post by:
Hello, I'm currently learning string manipulation. I'm curious about what is the favored way for string manipulation in C, expecially when strings contain non-ASCII characters. For example, if...
3
by: Darrel | last post by:
using Request.FilePath will return the entire file path: directory/directory/myfile.aspx Is there a built function in ASP.net to grab just the filename (myfile.aspx) or do I need to write...
3
by: crprajan | last post by:
String Manipulation: Given a string like “This is a string”, I want to remove all single characters( alphabets and numerals) like (a, b, 1, 2, .. ) . So the output of the string will be “This is...
12
by: snow | last post by:
Hi All, I noticed if file path has a white space, for example "C:\my document \test.txt", the function File.Exists(filePath) always return false in release mode. How could I make this function...
3
by: frankeljw | last post by:
I have 2 Java strings 1st String is a series of names, colons, and numbers ie) Name1:13:Name2:4526:Name3:789:Name4:3729:Name5:6:Name6:44 2nd String is a name ie) Name2 I need to get the...
2
by: AdrianJ1980 | last post by:
Hi all, Apologies if this has been discussed before, but my numerous searches (google and otherwise) have come up with nothing. What i need to do is open a word document (from Access 2003),...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.