美文网首页
python-exercise-2

python-exercise-2

作者: mihope | 来源:发表于2019-01-17 17:19 被阅读2次
    #!/usr/bin/python
    # -*- coding: utf-8 -*-
    import os
    
    
    def rename_file(dir_path, name_dict):
        files = os.listdir(dir_path)
        for file in files:
            old_file = os.path.join(dir_path, file)
            if os.path.isdir(old_file):
                continue
            filename = os.path.splitext(file)[0]
            file_type = os.path.splitext(file)[1]
            if filename in name_dict:
                new_name = name_dict[filename]
                new_file = os.path.join(dir_path, new_name + file_type)
                os.rename(old_file, new_file)
            else:
                print("%s%s 数据库里没有" % (filename, file_type))
    
    
    def read_json(json_path):
        kv_map = {}
        with open(json_path) as reader:
            for line in reader:
                if not line:
                    break
                kv = str(line.rstrip()).split(',')
                kv_map[kv[0]] = kv[1]
        return kv_map
    
    
    # 数据库数据
    json_file = "pre.txt"
    # 目录
    html_directory = "2019-01-16"
    print(html_directory)
    rename_file(html_directory, read_json(json_file))
    
    

    相关文章

      网友评论

          本文标题:python-exercise-2

          本文链接:https://www.haomeiwen.com/subject/qfaidqtx.html