czwartek, 28 listopada 2013

check if file is empty - python


#!/usr/bin/python
import os
check = os.stat("testfile")[6]==0
if check == True:
    print "file is empty"
else:
    print "open file"
    with open("testfile", "rt") as in_file:
        text = in_file.read()
    print (text)


python
Python 2.7.5 (default, Nov 12 2013, 16:18:42)
[GCC 4.8.2 20131017 (Red Hat 4.8.2-1)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.stat("testfile")
posix.stat_result(st_mode=33204, st_ino=5113343, st_dev=2056L, st_nlink=1, st_uid=1000, st_gid=1000, <i><b>st_size=0</b></i>, st_atime=1385626955, st_mtime=1385627001, st_ctime=1385627001)

Brak komentarzy:

Prześlij komentarz