[已解决] AttributeError: module ‘os‘ has no attribute ‘mknod‘

在训练模型前,根据模型文件存放地址判断文件是否存在,当文件不存在时,通过os.mknod()方法创建空文件,然后系统会报错

store_path = base_path + NetType + '.pt'
    if not os.path.exists(store_path):
        print("模型不存在!建立空模型")
        os.mknod(store_path)
    #     os.makedirs(store_path)
    print("模型存储路径:" + store_path)

 网上普遍的回答大多是 windows 系统不支持 mknod 而 Ubuntu 系统上没有问题

解决方法是 使用open("xxxx",mode = "w",encoding=None)

store_path = base_path + NetType + '.pt'
    if not os.path.exists(store_path):
        print("模型不存在!建立空模型")
        open(store_path,mode = "w",encoding=None)
    #     os.makedirs(store_path)
    print("模型存储路径:" + store_path)

 w 的用法可见这篇博客:

(130条消息) Python 打开文件 with open() 方法中文含义及‘r‘、‘w‘、‘x‘、‘a‘、‘b‘、‘t‘、‘+‘、‘U‘_daphne odera�的博客-CSDN博客

报错1: TypeError: an integer is required (got type str)

open方法内添加 encoding=None 或 encoding=“utf-8”

报错2:TypeError: open() missing required argument 'flags' (pos 2)

将 from os import open 改为 from io import open

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值