[Python] Python统计文件夹下所有文件名与内容 →→→→→进入此内容的聊天室

来自 , 2020-04-19, 写在 Python, 查看 189 次.
URL http://www.code666.cn/view/8452a95c
  1. #!/usr/bin/env python
  2. #coding=UTF-8
  3. #Author: lisunlou
  4. #Site: www.lisunlou.com
  5. #FileName: confile.py
  6.  
  7. import getopt, os, sys
  8.  
  9. def con_user(path):
  10.     filelist = os.listdir(path)
  11.     file_content = open('content.txt','w')
  12.     for user in filelist:
  13.         user_file = os.path.join(path, user)
  14.         if os.path.isfile(user_file):
  15.             file_user = open(user_file)
  16.             password = file_user.read()
  17.             file_s = 'filename:' + user + '\n'
  18.             content_s = 'content:' + password + '\n'
  19.             file_content.write(file_s + content_s)
  20.     file_content.close()
  21.     print '''The file "content.txt" is saved!'''
  22.  
  23. def usage():
  24.     print """USAGE: con_user.py content_dirs
  25.    The content result will saved as content.txt"""
  26.  
  27. if __name__ == "__main__":
  28.     try:
  29.         opts, args = getopt.getopt(sys.argv[1:],'',[])
  30.     except getopt.GetoptError:
  31.         usage()
  32.         sys.exit(0)
  33.     if not args:
  34.         usage()
  35.         sys.exit(0)
  36.     else:
  37.         for arg in args:
  38.             if os.path.isdir(arg):
  39.                 con_user(arg)
  40.             else:
  41.                 usage()
  42.                 print "content_dirs muest a dir!"
  43.                 sys.exit(0)
  44.  
  45. #//python/4434

回复 "Python统计文件夹下所有文件名与内容"

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

captcha