|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Generate data entry and reporting .NET Web apps in minutes, straight from your database. Read our FREE whitepaper “Build Web 2.0 Applications Without Hand-Coding” Download now! |
|
#1
|
|||
|
|||
|
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? |
|
#2
|
|||
|
|||
|
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
|
|
#3
|
|||
|
|||
|
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 |
|
#4
|
|||
|
|||
|
It's not an example, it's a builtin function. It should definitely work. You might need to import the os module though.
|
|
#5
|
||||
|
||||
|
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. |
|
#6
|
|||
|
|||
|
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 ![]() |
|
#7
|
||||
|
||||
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. |
|
#8
|
|||
|
|||
|
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? |
|
#9
|
||||
|
||||
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. |
|
#10
|
|||
|
|||
|
thanx! i tried the escaping b4 but then it didn't work. now it does :P
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > get filename from long path |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|