python 读写文件问题(提问者已疯)

def get_video_info(video_name):
    """
    get width, height, frames information of video
    :param video_name: video
    :return:
    """
    name = os.path.basename(video_name).split(".")[0]
    json_file = "./" + name + ".json"
    # Does the reason that add the sudo command in the front result in program stop?
    cmd = ("ffprobe -print_format json -show_format -show_streams -i " +
           video_name + " > " + json_file)
    # 这一块的过程为: 使用subprocess.Popen将cmd的运行结果存储到相应的json文件,如cmd中的">",
    # 然后再由open函数打开文件并转换为python dict格式
    # 1. 第一次(未生成json文件)执行, 报错,再assert语句中, getsize函数未找到文件"No such file or directory",
    # 项目目录中有该文件且文件中有内容.
    # 2. 第二次执行(未删除json文件直接执行), 结果输出正常,打开文件发现文件为空,打开终端到json文件所在目录下,
    # 运行getsize报错"No such file or directory"
    # 3. 第三次运行(未删除json文件直接执行), assert语句报错,打开json文件发现有内容, 终端到json文件所在目录下,
    # 运行getsize报错"No such file or directory", vim打开json有内容
    # 4. 第四次运行(未删除json文件直接执行), 和2一样
    # 5. 第五次运行(未删除json文件直接执行), 和3一样
    # 6. 第六次运行(未删除json文件直接执行), 和2一样
    # ......
    subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE)
    assert os.path.getsize(json_file) > 0
    with open(json_file, 'r', encoding='utf8') as f:
        info = f.read()
        print(info)
        info_dict = json.loads(info)
    width = info_dict["streams"][0]["width"]
    height = info_dict["streams"][0]["height"]
    fps = info_dict["streams"][0]["r_frame_rate"]
    print(width, height, fps)
    return width, height, fps

应该是内存写入磁盘的问题,但具体不了解,求教

解决了,就是文件连续写读的问题, 写文件的操作还未把数据从写入磁盘,下一行代码就去读取了

把错误提示贴出来看看,要不然不好分析呀

您好,我是有问必答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632