Skip to content
Advertisement

Python String .strip() function returning wrong output

I have the following string

'file path = data/imagery/256:0:10.0:34:26:-1478/256:0:10.0:34:26:-1478_B02_10m.tif'

I am trying to get 256:0:10.0:34:26:-1478_B02_10m.tif from the string above

but if I run

os.path.splitext(filepath.strip('data/imagery/256:0:10.0:34:26:-1478'))[0]

It outputs '_B02_10m'

Same with filepath.rstrip('data/imagery/256:0:10.0:34:26:-1478')

Advertisement

Answer

Assuming you want all the string data after the / you can always use string.split. This spits your string into a list of strings split on the split string. Then you would only need the final item of this list.

string_var.split("/")[:-1]

See more official python docs on string.split here.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement