I’m looking for a simple method of identifying the last position of a string inside another string … for instance. If I had: file = C:\Users\User\Desktop\go.py
and I wanted to crop this so that file = go.py
Normally I would have to run C:\Users\User\Desktop\go.py through a loop + find statement, and Evey time it encountered a \ it would ask … is the the last \ in the string? … Once I found the last \ I would then file = file[last\:len(file)]
I’m curious to know if there is a faster neater way to do this.. preferably without a loop.
Something like file = [file('\',last):len(file)]
If there is nothing like what I’ve shown above … then can we place the loop inside the [:] somehow. Something like file = [for i in ...:len(file)]
thanks 🙂
If it is only about file paths, you can use
os.path.basename:Or if you are not running the code on Windows, you have to use
ntpathinstead ofos.path.