The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
get filename from long path
Discuss get filename from long path in the Python Programming forum on Dev Shed. get filename from long path Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

September 3rd, 2003, 07:31 AM
|
|
Junior Member
|
|
Join Date: Sep 2003
Posts: 5
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?
|

September 3rd, 2003, 07:33 AM
|
|
Contributing User
|
|
Join Date: Jul 2003
Posts: 133
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
|

September 3rd, 2003, 07:49 AM
|
|
Junior Member
|
|
Join Date: Sep 2003
Posts: 5
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
|

September 3rd, 2003, 08:02 AM
|
|
Contributing User
|
|
Join Date: Jul 2003
Posts: 133
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.
|

September 3rd, 2003, 08:08 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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.
|

September 3rd, 2003, 09:54 AM
|
|
Junior Member
|
|
Join Date: Sep 2003
Posts: 5
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 
|

September 3rd, 2003, 11:49 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
 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.
|

September 4th, 2003, 09:01 AM
|
|
Junior Member
|
|
Join Date: Sep 2003
Posts: 5
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?
|

September 4th, 2003, 10:17 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
 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.
|

September 5th, 2003, 04:24 AM
|
|
Junior Member
|
|
Join Date: Sep 2003
Posts: 5
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
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|