#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    5
    Rep 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?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    133
    Rep Power
    12
    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
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    5
    Rep 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
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    133
    Rep Power
    12
    It's not an example, it's a builtin function. It should definitely work. You might need to import the os module though.
  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    5
    Rep 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
  12. #7
  13. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
  14. #8
  15. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    5
    Rep 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?
  16. #9
  17. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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 11:23 AM.
  18. #10
  19. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2003
    Posts
    5
    Rep Power
    0
    thanx! i tried the escaping b4 but then it didn't work. now it does :P
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    1
    Rep Power
    0
    You can use Long Path Tool as well, it works good for such problems!
  22. #12
  23. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    620
    Rep Power
    65
    Needs to be raw string ...
    Code:
    path = r"C:\Work\nieuwsbrief\brief1\agradicat.jpg"
    
    fname = path.split('\\')[-1]
    print(fname)
    Actually, Windows does accept
    "C:/Work/nieuwsbrief/brief1/agradicat.jpg"
    Last edited by Dietrich; December 6th, 2013 at 12:56 PM.
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2014
    Posts
    1
    Rep Power
    0

    long path files


    Long Path Tool works excellent to copy or delete, long path files quickly than other software. You may also try it and I m sure you will be happy too. Thank you!

IMN logo majestic logo threadwatch logo seochat tools logo