display an error message when file is empty - proper way?
Question: hi im slowly trying to learn the correct way to write python code. suppose i
have a text file which i want to check if empty, what i want to happen is that
the program immediately terminates and the console window displays an error
message if indeed empty. so far what ive done is written below. please teach
me the proper method on how one ought to handle this case:

    
    
    import os
    
        def main():
    
            f1name = 'f1.txt'
            f1Cont = open(f1name,'r')
    
            if not f1Cont:
                print '%s is an empty file' %f1name
                os.system ('pause')
    
            #other code
    
        if __name__ == '__main__':
            main()
    


Answer: There is no need to `open()` the file, just use
[`os.stat()`](http://docs.python.org/library/os.html#os.stat).

    
    
    >>> #create an empty file
    >>> f=open('testfile','w')
    >>> f.close()
    >>> #open the empty file in read mode to prove that it doesn't raise IOError
    >>> f=open('testfile','r')
    >>> f.close()
    >>> #get the size of the file
    >>> import os
    >>> import stat
    >>> os.stat('testfile')[stat.ST_SIZE]
    0L
    >>>
    

