Check that a *type* of file exists in Python -
i realize looks similar other questions checking if file exists, different. i'm trying figure out how check type of file exists , exit if doesn't. code tried this:
filenames = os.listdir(os.curdir) filename in filenames: if os.path.isfile(filename) , filename.endswith('.fna'): ##do stuff
this works 'do stuff' file ends in .fna, need check , make sure .fna file there , exit program entirely if not.
i tried this:
try: if os.path.isfile(filename) , filename.endswith('.fna'): ## stuff except: sys.stderr.write (‘no database file found. exiting program. /n’) sys.exit(-1)
but didn't work, skips whole function if .fna file isn't there, without printing error.
the for
statement in python has little-known else
clause:
for filename in filenames: if os.path.isfile(filename) , filename.endswith(".fna"): # stuff break else: sys.stderr.write ('no database file found. exiting program. \n') sys.exit(-1)
the else
clause run only if for
statement runs through whole enumeration without executing break
inside loop.
Comments
Post a Comment