Python Forum
extract last word from path.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
extract last word from path.
#3
I usually simply split and slice:
path = "D:\Python\sample_folder\data"
lastword = path.split('\\')[-1]
print(lastword)
Output:
>>> path = "D:\Python\sample_folder\data" >>> lastword = path.split("\\")[-1] >>> print(lastword) data >>>
Reply


Messages In This Thread
extract last word from path. - by mg24 - Nov-01-2022, 09:13 AM
RE: extract last word from path. - by menator01 - Nov-01-2022, 10:11 AM
RE: extract last word from path. - by Larz60+ - Nov-01-2022, 10:12 AM
RE: extract last word from path. - by mg24 - Nov-01-2022, 10:39 AM
RE: extract last word from path. - by carecavoador - Nov-01-2022, 10:55 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  extract substring from a string before a word !! evilcode1 3 645 Nov-08-2023, 12:18 AM
Last Post: evilcode1
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 2,280 Sep-09-2021, 01:25 PM
Last Post: Yoriz
Question Problem: Check if a list contains a word and then continue with the next word Mangono 2 2,598 Aug-12-2021, 04:25 PM
Last Post: palladium
  How to extract a single word from a text file buttercup 7 3,793 Jul-22-2020, 04:45 AM
Last Post: bowlofred
  Python Speech recognition, word by word AceScottie 6 16,184 Apr-12-2020, 09:50 AM
Last Post: vinayakdhage
  Extract Httponly, Secure, domain and path from a cookie randeniyamohan 0 1,719 Jan-26-2020, 04:59 PM
Last Post: randeniyamohan
  print a word after specific word search evilcode1 8 5,022 Oct-22-2019, 08:08 AM
Last Post: newbieAuggie2019
  .pth file does not show up in sys.path when configuring path. arjunsingh2908 2 5,883 Jul-03-2018, 11:16 AM
Last Post: arjunsingh2908
  difference between word: and word[:] in for loop zowhair 2 3,770 Mar-03-2018, 07:24 AM
Last Post: zowhair

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020