[Python] python读取目录下文件并生成日志 →→→→→进入此内容的聊天室

来自 , 2021-04-10, 写在 Python, 查看 157 次.
URL http://www.code666.cn/view/acef5cc0
  1. import os  
  2. from time import strftime  
  3. stamp=strftime("%Y-%m-%d %H:%M:%S")  
  4. logfile = 'F://test//m-php-framework//tmp/logs//error_report.log'  
  5. path = 'F://test//'  
  6. files = os.listdir(path)  
  7. bytes = 0  
  8. numfiles = 0  
  9. for f in files:  
  10.     if f.startswith('t'):  
  11.         info = os.stat(path + f)  
  12.         numfiles +=1  
  13.         bytes+=info[6]  
  14. if numfiles > 1:  
  15.     title = 'tiles'  
  16. else:  
  17.     title = 'file'  
  18. string = stamp + " -- " + str(numfiles) + " session" +title+","+ str(bytes)+" bytes/n"  
  19. file = open(logfile,"a")  
  20. file.writelines(string)  
  21. file.close()  
  22. #//python/1157

回复 "python读取目录下文件并生成日志"

这儿你可以回复上面这条便签

captcha