自己写的showFileProperty函数 结构化输出文件属性
import os,sys
def showFileProperties(path):
fi=open(r"./FileProperty.txt","a+")
fi.write("%-90s%-20s%-20s%-20s%-20s%-20s\n" %("Filename","Size","CreateTime","ModifyTime","AccessTime","Mode"))
for root,dirs,files in os.walk(path,True):
for filename in files:
fn=os.path.join(root,filename)
state=os.stat(fn)
fi.write("%-90s" %fn)
fi.write("%-20s" %str(state.st_size))
fi.write("%-20s" %str(state.st_ctime))
fi.write("%-20s" %str(state.st_mtime))
fi.write("%-20s" %str(state.st_atime))
fi.write("%-20s" %str(state.st_mode))
fi.write("\n")
fi.close()
if __name__=="__main__":
path=r"D:\TDDOWNLOAD"
showFileProperties(path)
总结起来收获:
1.写文件的write还可以这样用,特别是-号左对齐
2.输出属性的时候总是不对,后来加上str才对,因为看了个帖子,他说输出0,不能用fi.write(0),必须用fi.write(str(0)),照葫芦画瓢。。。
3.有进步