加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

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

发布时间:2020-12-17 17:28:05 所属栏目:Python 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 #!/usr/bin/python # -*- coding:utf8 -*-#该脚本是检查客户端同事给的TXT文件是否支持JSON格式#只有通过后,才能交给运维同事处理抓取视频import osi

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

#!/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
    

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读