以前自己写的一段笨代码,将一堆东西统计到了一个文件夹下,现在拿来看了一下已经搜集了不少资料(至于是什么东西不方便多说),但是阅读起来相当麻烦,所以再次写了一个简单的Python脚本,将所有的文件名和文件中的内容整理一下,分享一下(只测试过Linux,理论上Windows和Mac不会有任何问题):
转自:http://www.lisunlou.com/archives/539
#!/usr/bin/env python #coding=UTF-8 #Author: lisunlou #Site: www.lisunlou.com #FileName: confile.py import getopt, os, sys def con_user(path): filelist = os.listdir(path) file_content = open('content.txt','w') for user in filelist: user_file = os.path.join(path, user) if os.path.isfile(user_file): file_user = open(user_file) password = file_user.read() file_s = 'filename:' + user + '\n' content_s = 'content:' + password + '\n' file_content.write(file_s + content_s) file_content.close() print '''The file "content.txt" is saved!''' def usage(): print """USAGE: con_user.py content_dirs The content result will saved as content.txt""" if __name__ == "__main__": try: opts, args = getopt.getopt(sys.argv[1:],'',[]) except getopt.GetoptError: usage() sys.exit(0) if not args: usage() sys.exit(0) else: for arg in args: if os.path.isdir(arg): con_user(arg) else: usage() print "content_dirs muest a dir!" sys.exit(0)