python分析列表文件的内容是否支持JSON

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

#!/usr/bin/python  
# -*- coding:utf8 -*-
#该脚本是检查客户端同事给的TXT文件是否支持JSON格式
#只有通过后,才能交给运维同事处理抓取视频

import os
import sys
import 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_files

  
if __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