I’m attempting to use a Python code block in Zapier to check if a file name ends with a specific extension. My aim is to eliminate that extension if it is present; otherwise, I want the original filename to remain unchanged.
Interestingly, my current code seems to function only when I incorporate .lower() into it, and I’m puzzled as to why that’s necessary. Here’s my code snippet:
I’ve explored various methods, but I can’t seem to get any of them to work without using the .lower() function. I really prefer to keep the original casing of the filename intact rather than changing everything to lowercase. If anyone has faced this problem, I would greatly appreciate your help with a solution.
This is a Zapier quirk, not a Python issue. Zapier sometimes adds hidden encoding inconsistencies or trailing characters to file names that you can’t see. Your code should work fine without the .lower() in the return statement. Try this: if file_name.strip().lower().endswith(‘.mov’): file_name = file_name.strip()[:-4] then return the original casing. The .strip() catches any hidden whitespace that’s messing with your string operations. I’ve seen this with other automation platforms too - they slightly modify data between steps, which makes standard string methods act weird.
totally agree, the .lower() shouldn’t be necessary. maybe check for hidden stuff in your filename, like spaces or other chars that could mess with it. try using print(repr(file_name)) to see what’s up. endswith() usually does the job on its own.
Had this exact same issue a few months ago. The problem? Case sensitivity. .endswith() is case-sensitive, so if your file is “video.MOV” instead of “video.mov”, it won’t match and the extension stays put. Don’t force everything to lowercase in the return - just make the comparison case-insensitive: if file_name.lower().endswith('.mov'): but keep the original casing in your slice. You’ll preserve the original filename formatting while catching extensions no matter how they’re capitalized. This happens because files from different systems have extensions capitalized differently.