vkaul11
vkaul11

Reputation: 4214

stripping a pattern from the end of the string

I want to see if a file like test_100.webp exists and then look at the file test.yaml. Therefore, I need to strip the pattern "_100.webp" from the end. I tried to use the code below and it is giving me issues.

 for i, image in enumerate(images_in_item):
        if image.endswith("_100.webp"):
            image_strip = image.rstrip(_100.webp)
            snapshot_markup = os.path.join(image_strip + 'yaml')

Upvotes: 0

Views: 69

Answers (1)

user25148
user25148

Reputation:

Do this:

suffix = '_100.webp'
if image.endswith(suffix):
    image_strip = image[:-len(suffix)]
    snapshot_markup = os.path.join(image_strip + 'yaml')

Upvotes: 1

Related Questions