Correct way to check for empty or missing file in Python -


i want check both whether file exists and, if does, if empty.

if file doesn't exist, want exit program error message.

if file empty want exit different error message.

otherwise want continue.

i've been reading using try: except: i'm not sure how structure code 'pythonically' achieve i'm after?


thank responses, went following code:

try:     if os.stat(urlfilepath + urlfile).st_size > 0:         print "processing..."     else:         print "empty url file ... exiting"         sys.exit() except oserror:     print "url file missing ... exiting"     sys.exit() 

i'd use os.stat here:

try:     if os.stat(filename).st_size > 0:        print "all good"     else:        print "empty file" except oserror:     print "no file" 

Comments