Sunday, January 18, 2009

python获得并输出文件属性






python获得并输出文件属性
代码:
import os
import time
file = "samples/sample.jpg"
def dump(st):
    mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime = st
    print "- size:", size, "bytes"
    print "- owner:", uid, gid
    print "- created:", time.ctime(ctime)
    print "- last accessed:", time.ctime(atime)
    print "- last modified:", time.ctime(mtime)
    print "- mode:", oct(mode)
    print "- inode/dev:", ino, dev
# get stats for a filename
st = os.stat(file)
print "station", file
dump(st)
print st

# get stats for an open file
fp = open(file)
st = os.fstat(fp.fileno())
print "fstat", file
dump(st)

print st.st_ctime


201865413.8952832



import os
>>> o=os.stat(r'F:\\Xunlei\160103.doc')
>>> t=o.st_ctime
>>> t
1202624180.046875
>>> print type(t)
<type 'float'>
>>> import time
>>> r=time.ctime(t)
>>> r
'Sun Feb 10 14:16:20 2008'
>>> s=time.gmtime(t)
>>> s
(2008, 2, 10, 6, 16, 20, 6, 41, 0)
>>> time.strftime("%a, %d %b %Y %H:%M:%S +0000", s)
'Sun, 10 Feb 2008 06:16:20 +0000'