On the use of judgment sentences in Python

I wrote a method as follows:

file = None
for f in os.listdir(os.getcwd()):
    if os.path.splitext(f)[1] == ".*" and os.path.splitext(f)[0] == os.getenv("") or "text":
        file = f
return file
The

expectation is that if the file does not exist, it returns None , which actually returns _ _ pycache__ .

I modified the method as follows:

file = None
for f in os.listdir(os.getcwd()):
    if os.path.splitext(f)[1] == ".*":
        if os.path.splitext(f)[0] == os.getenv("") or "text":
            file = f
return file

this returns the expected value None .

excuse me, why?

Mar.28,2021

and takes precedence over or

your first method is slightly modified,

file = None
for f in os.listdir(os.getcwd()):
    if os.path.splitext(f)[1] == '.*' and (os.path.splitext(f)[0] == os.getenv('') or 'text'):
        file = f
return file

should meet your expectations.

Menu