You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python类字段在函数中未正确初始化致路径生成异常求助

问题与解决方案

问题描述

编写了一个检查路径是否为目录的Python函数,路径依赖类中wafer_num字段,但函数返回False。生成的路径为:

/usr/local/insight/results/images/toolsDB/lauto_ptest_s2022-08-09/w<class 'int'>

而非预期的/usr/local/insight/results/images/toolsDB/lauto_ptest_s2022-08-09/w+数字(例如wafer_num为2时,路径应为/usr/local/insight/results/images/toolsDB/lauto_ptest_s2022-08-09/w2)。其他依赖该字段的函数运行正常,相关代码如下:

调用代码(其他文件)

import NR_success_check_BBs as check_BBs
# 已实现的配方列表:
sanity = 'sanity_' + time_now + '_Ver_5.1'
R2M_delayer = 'R2M_E2E_delayer_NR_Ver_5.1'
R2M_cross_section = 'R2M_E2E_NR_Ver_5.1'

# 调用构造函数创建包含配方数据的对象
check_BBs.set_recipe_name(R2M_delayer)

# 执行对象检查逻辑
check_BBs.success_check()

核心代码文件

import datetime
import logging
import os.path
from pathlib import Path
import time

time_now = str(datetime.datetime.now()).split()[0]
log_file_name_to_write = 'test.log'
log_file_name_to_read = 'NR_' + time_now + '.log'
logging.basicConfig(level=logging.INFO, filename=log_file_name_to_write, filemode='a',format='%(asctime)s - %(levelname)s - %(message)s')
R2M_RecipeRun_path = '/usr/local/disk2/unix_nt/R2M/RecipeRun/'
R2M_dir = '/usr/local/disk2/unix_nt/R2M'
metro_callback_format = 'Metro_Reveal_' 

class recipe:
    name = ''
    script_name = ''
    wafer_num = int  # 初始值为int类型对象,而非数值
    images = 0
    metrology_images = 0

# 此处初始化时recipe.wafer_num还是int类型,会导致路径错误,但用户问题出在检查函数
images_output_dir_path = '/usr/local/insight/results/images/toolsDB/lauto_ptest_s/' + time_now + 'w' + str(recipe.wafer_num)

def set_recipe_name(name):
    recipe.name = name
    logging.info("Analyzing the recipe: " + recipe.name)

    if recipe.name == 'sanity_' + time_now + '_Ver_5.1':
        recipe.script_name = 'Sanity_CS.py'
        recipe.wafer_num = 1

    elif recipe.name == 'R2M_E2E_NR_Ver_5.1':
        recipe.script_name = 'R2M.py'
        recipe.wafer_num = 2

    elif recipe.name == 'R2M_E2E_delayer_NR_Ver_5.1':
        recipe.script_name = 'R2M_delayer.py'
        recipe.wafer_num = 3

# 出问题的函数
def is_results_images_directory_exist():
    images_directory_path = Path('/usr/local/insight/results/images/toolsDB/lauto_ptest_s' + time_now + '/w' + wafer_num)
    print(images_directory_path)
    is_directory = os.path.isdir(images_directory_path)
    print(is_directory)
    if not is_directory:
        logging.error("There is no images directory for the current recipe at results")
        return False
    return True

错误原因分析

  1. 变量引用错误:is_results_images_directory_exist函数中直接使用wafer_num,但该变量未定义,应该引用类属性recipe.wafer_num。
  2. 类型转换缺失:即使引用正确,若未将recipe.wafer_num转为字符串,直接拼接会报错(整数无法与字符串拼接)。
  3. 初始值不当:recipe类中wafer_num初始值设为int(类型对象),虽然set_recipe_name会覆盖该值,但如果未调用该函数,会导致路径中出现<class 'int'>。

修复方案

1. 修正检查函数的变量引用与路径拼接

将is_results_images_directory_exist函数修改为:

def is_results_images_directory_exist():
    # 正确引用类属性,并转为字符串
    wafer_num_str = str(recipe.wafer_num)
    # 使用Path的/运算符拼接路径,更可靠
    images_directory_path = Path('/usr/local/insight/results/images/toolsDB') / f'lauto_ptest_s{time_now}' / f'w{wafer_num_str}'
    print(images_directory_path)
    is_directory = images_directory_path.is_dir()  # 直接用Path的is_dir方法,无需os.path
    print(is_directory)
    if not is_directory:
        logging.error("当前配方在results目录下没有对应的图片文件夹")
        return False
    return True

2. 优化类的初始值

将recipe类的wafer_num初始值改为数值类型,避免潜在问题:

class recipe:
    name = ''
    script_name = ''
    wafer_num = 0  # 改为0而非int类型对象
    images = 0
    metrology_images = 0

3. 修正全局路径变量(可选)

如果images_output_dir_path需要正确的路径,应在set_recipe_name之后定义,或者改为动态获取:

# 移除原来的全局定义,改为函数内动态生成或在set_recipe_name后赋值
def get_images_output_dir():
    return Path('/usr/local/insight/results/images/toolsDB/lauto_ptest_s') / time_now / f'w{recipe.wafer_num}'

验证

修复后,当调用set_recipe_name(R2M_delayer)时,recipe.wafer_num被设为3,生成的路径会是/usr/local/insight/results/images/toolsDB/lauto_ptest_s2022-08-09/w3,符合预期,函数会正确检查目录是否存在。


内容的提问来源于stack exchange,提问作者I'm not a robot

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 21:39:17