Check that a *type* of file exists in Python

后端 未结 6 727
囚心锁ツ
囚心锁ツ 2021-01-11 17:03

I realize this looks similar to other questions about checking if a file exists, but it is different. I\'m trying to figure out how to check that a type of

6条回答
  •  既然无缘
    2021-01-11 17:13

    The for statement in Python has a little-known else clause:

    for filename in filenames:
        if os.path.isfile(filename) and filename.endswith(".fna"):
            # do stuff
            break
    else:
        sys.stderr.write ('No database file found. Exiting program. \n')
        sys.exit(-1)
    

    The else clause is run only if the for statement runs through its whole enumeration without executing the break inside the loop.

提交回复
热议问题