I have a string representing a filename, how can I check it against few conditions at once? I've tried
if r'.jpg' or r'.png' not in singlefile:
but kept getting false positives all the time.
Your code is equal with:
if (r'.jpg') or (r'.png' not in singlefile):
You're, probably, looking for:
if r'.jpg' not in singlefile or r'.png' not in singlefile:
Or
if any(part not in singlefile for part in [r'.jpg', r'.png']):
Thanks to Tim Pietzcker:
He(you) actually (probably) wants
if not any(singlefile.endswith(part) for part in [r'.jpg', r'.png']) # ^^^^^^^^
if not any(singlefile.endswith(part) for part in [r'.jpg', r'.png']).endswith is probably the best solutionThis is because of precedence. Following code means.
# r'.jpg' is constant
if r'.jpg' or (r'.png' not in singlefile):
If constant, or .png not in singlefile. As constant is always true, the expression is always truthy.
Instead, you could try using regular expressions, to check whatever string fits a pattern.
import re
if re.match(r"\.(?:jpg|png)$", singlefile):