如何使用 Python 代码修改 JSON 文件中的字段并复制文件夹中的文件到新路径?

花韻仙語
发布: 2024-11-09 18:18:23
原创
707人浏览过

如何使用 python 代码修改 json 文件中的字段并复制文件夹中的文件到新路径?

修改 json 和复制文件夹中的文件到新路径

以下 python 代码可用于修改 json 文件中的部分字段,并复制文件夹中的 json 文件、jpg 文件和 avi 文件到新路径。

import os
import json
import shutil

# 获取 json 文件的路径
def get_json_data(old_json_path):
    json_file_paths = []
    for root, dirs, files in os.walk(old_json_path):
        for file_name in files:
            if file_name.endswith('.json'):
                old_json_file_path = os.path.join(root, file_name)
                json_file_paths.append(old_json_file_path)
    return json_file_paths

# 获取文件夹的路径
def get_dir_name(dir_name):
    dir_names = []
    for root, dirs, files in os.walk(dir_name):
        for dir in dirs:
            dirpath = os.path.join(root, dir)
            dir_names.append(dirpath)
    return dir_names

# 读取 json 文件并更新字段
def read_json(filepath):
    with open(filepath, 'r', encoding='utf-8') as f:
        json_data = f.read()
        if json_data:
            json_data_dict = eval(json_data)
            json_data_dict.update({"filename": filepath.split('\')[-2].replace('.dav', '.avi')})
    return json_data_dict

# 写入 json 文件
def write_json(filepath, newdata):
    with open(filepath, 'w', encoding='utf-8') as r:
        json.dump(newdata, r, indent=4, ensure_ascii=false)

# 复制 json 文件和其他文件到新路径
def copy_files(old_path, new_path):
    dir_names = get_dir_name(old_path)
    for dir in dir_names:
        json_file_list = get_json_data(dir)
        for json_file in json_file_list:
            dir_name = dir.split('\')[-1].replace('.dav', '.avi')
            new_dir_name = os.path.join(new_path, dir_name)
            if not os.path.exists(new_dir_name):
                os.mkdir(new_dir_name)
            shutil.copy(json_file, new_dir_name)
            newdata = read_json(new_dir_name + '\' + os.path.basename(json_file))
            write_json(new_dir_name + '\' + os.path.basename(json_file), newdata)
        other_files = []
        for root, dirs, files in os.walk(old_path):
            for file_name in files:
                if file_name.endswith('.jpg') or file_name.endswith('.avi'):
                    other_files.append(os.path.join(old_path, file_name))
        for file in other_files:
            shutil.copy(file, new_path)

# 主函数
if __name__ == "__main__":
    old_path = r'd:ss'
    new_path = r'd:json'
    copy_files(old_path, new_path)
登录后复制

eof 错误解决

eof 错误是因为尝试在文件末尾执行 eval(json_data),而此时 json_data 为空。可以通过检查 json_data 是否为空来避免此错误。

立即学习Python免费学习笔记(深入)”;

import os
import json

file_path = r'F:scode_pjpj_test	est_data.json'

with open(file_path) as f:
    data = f.read()
    if data:
        b = eval(data)
登录后复制

以上就是如何使用 Python 代码修改 JSON 文件中的字段并复制文件夹中的文件到新路径?的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
相关标签:
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号