python分析列表文件的内容是否支持JSON,,#!/usr/bin/p


#!/usr/bin/python  # -*- coding:utf8 -*-#该脚本是检查客户端同事给的TXT文件是否支持JSON格式#只有通过后,才能交给运维同事处理抓取视频import osimport sysimport json#遍历指定目录下所有的文件名称def get_recursive_file_list(path):    current_files = os.listdir(path)    all_files = []    for file_name in current_files:        full_file_name = os.path.join(path, file_name)        all_files.append(full_file_name)        if os.path.isdir(full_file_name):            next_level_files = get_recursive_file_list(full_file_name)            all_files.extend(next_level_files)    return all_filesif __name__ == '__main__':      arg = sys.argv[1]   #获取命令行的第一个参数    path = '../video/' + arg    all_flies = get_recursive_file_list(path)      for file in all_flies:         if os.path.isfile(file):            file_object = open(file)            try:                all_text = file_object.read()                try:#如果json解析不了,则抛出异常                    arr = json.loads(all_text)#                     for a in arr:#                         for (k,v) in a.items():#                             print k+'='+ unicode(v)                except:                    print "错误的文件格式:"+file            finally:                file_object.close()         else:            pass

评论关闭