Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 3rd, 2003, 07:31 AM
ijsman ijsman is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 5 ijsman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
get filename from long path

i have this string

myfilename = "C:\Work\nieuwsbrief\brief1\agradicat.jpg"

then i want to get the filename only out of there and no path.
i tried splitting on '\' but that doesn't work, then i searched google and found the following script

id = myfilename[max(string.rfind(filename, '/'),string.rfind(filename, '\'),string.rfind(filename, ':'),)+1:]

this has errors and since i don't know how rfind works maybe someone here can help me?

Reply With Quote
  #2  
Old September 3rd, 2003, 07:33 AM
percivall percivall is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 133 percivall User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 10
From the built-in help:

Code:
Help on function basename in os.path:

os.path.basename = basename(p)
    Returns the final component of a pathname

Reply With Quote
  #3  
Old September 3rd, 2003, 07:49 AM
ijsman ijsman is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 5 ijsman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
eeh, sorry doesn't work for me

I'm using Zope and the example you gave is for python stand alone i think.

if is use your code i get global name basename is not defined
also i have to enter a path manually and it should be passed to the function from the script

Reply With Quote
  #4  
Old September 3rd, 2003, 08:02 AM
percivall percivall is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 133 percivall User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 10
It's not an example, it's a builtin function. It should definitely work. You might need to import the os module though.

Reply With Quote
  #5  
Old September 3rd, 2003, 08:08 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Hi ijsman, this sould work fine.. (the following is an interactive Python session and shows the steps taken)

>>> import os
>>> os.path.basename('c:/windows/desktop/python.py')
'python.py'
>>>

What percilvall gave you was Python's help() function output for os.path.basename it wasnt actually ment to work.

Have fun,
Mark.

Reply With Quote
  #6  
Old September 3rd, 2003, 09:54 AM
ijsman ijsman is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 5 ijsman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
in Zope i can't use python the way you do,

can't i use the rfind or split function? that would be much more usefull

Reply With Quote
  #7  
Old September 3rd, 2003, 11:49 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Sorry, I havnt done too much with Zope yet, it's something i've been meaning to get into but.. anyway if Zope is using Python 2.2+ you can do this..

>>> p = '/home/username/python.py'
>>> p.split('/')[-1]
'python.py'

-1 being the last entry in a list.. if you don't have Python 2.2+ you'll need to use the string module instead of the bult-in's

>>> import string
>>> p = '/home/username/python.py'
>>> string.split(p, '/')[-1]
'python.py'

One of these is bound to work..

Note: If your on windows you may need to change the split char' to '\\'. Also remember that the interactive session's (above) only show the step by step. >>> is the interpriters interactive prompt.. just incase you are copying and pasting. if you remove the >>> the examples above should work perfectly..

Hope this helps ,

have fun,
Mark.

Reply With Quote
  #8  
Old September 4th, 2003, 09:01 AM
ijsman ijsman is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 5 ijsman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
The problem is that i'm running under windows and that this script:

p.split('\')

doesn't work

whereas

p.split('/')

does work but is of no use to me.

the backslash split gives me a python error: 'invalid token'


so basically all i want to know is how to SPLIT a string with \ as the split delimiter.

Surely this is possible?

Reply With Quote
  #9  
Old September 4th, 2003, 10:17 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
You need to escape your backslash so it becomes '\\\'. When you use a backslash within a string you need to remember that it's a special char' used to escape other special chars i.e "I read that Zope is \"Python Powered\"" is the same as I read that Zope is "Python powered"

>>> import string
>>> p = "c:\\windows\\desktop\\python"
>>> string.split(p, "\\")
['c:', 'windows', 'desktop', 'python']

Mark.

Last edited by netytan : September 4th, 2003 at 10:23 AM.

Reply With Quote
  #10  
Old September 5th, 2003, 04:24 AM
ijsman ijsman is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 5 ijsman User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
thanx! i tried the escaping b4 but then it didn't work. now it does :P

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > get filename from long path

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap