mmcv读取配置文件,在windows上无法创建临时文件解决方法

报错是写入C盘的Temp没有权限,因此修改到其他目录.
在config中修改以下两个函数:

    def _substitute_predefined_vars(filename, temp_config_name):
        file_dirname = osp.dirname(filename)
        file_basename = osp.basename(filename)
        file_basename_no_extension = osp.splitext(file_basename)[0]
        file_extname = osp.splitext(filename)[1]
        support_templates = dict(
            fileDirname=file_dirname,
            fileBasename=file_basename,
            fileBasenameNoExtension=file_basename_no_extension,
            fileExtname=file_extname)
        config_file = open(filename).read()
        for key, value in support_templates.items():
            regexp = r'\{\{\s*' + str(key) + r'\s*\}\}'
            config_file = re.sub(regexp, value, config_file)
		######################################
        print(temp_config_name)
        temp_name = osp.dirname(temp_config_name)
        if not osp.exists(temp_name):  # 再次创建指定目录
            os.makedirs(temp_name)
        temp_config_name =os.path.join(temp_name, "1234.py")  # 保存到1234.py中
        ####################################
        with open(temp_config_name, 'w') as tmp_config_file:
            tmp_config_file.write(config_file)
 def _file2dict(filename, use_predefined_variables=True):
        filename = osp.abspath(osp.expanduser(filename))
        check_file_exist(filename)
        fileExtname = osp.splitext(filename)[1]
        if fileExtname not in ['.py', '.json', '.yaml', 'yml']:
            raise IOError('Only py/yml/yaml/json type are supported now!')
        #####################################
        temp_name = r"D:\chl_files\projects\ai_project\mmsegmentation\temp"
        # with tempfile.TemporaryDirectory() as temp_config_dir: 注释该行
        temp_dir = tempfile.TemporaryDirectory() #1
        if not osp.exists(temp_name):  #2
            os.makedirs(temp_name)  #3
        temp_dir.name = temp_name #4 修改目录到指定目录
        #####################################
        with temp_dir as temp_config_dir:
            temp_config_file = tempfile.NamedTemporaryFile(
                dir=temp_config_dir, suffix=fileExtname)
            temp_config_name = osp.basename(temp_config_file.name)
            # Substitute predefined variables
            if use_predefined_variables:
                Config._substitute_predefined_vars(filename,
                                                   temp_config_file.name)
            else:
                shutil.copyfile(filename, temp_config_file.name)

            if filename.endswith('.py'):  # 读取保存的文件1234.py
                temp_module_name = osp.splitext(temp_config_name)[0]
                temp_module_name = "1234"  # temp_config_name =os.path.join(temp_name, "1234.py")  # 
                sys.path.insert(0, temp_config_dir)
                Config._validate_py_syntax(filename)
                mod = import_module(temp_module_name)
                sys.path.pop(0)
                cfg_dict = {
                    name: value
                    for name, value in mod.__dict__.items()
                    if not name.startswith('__')
                }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值