import configparser # 创建配置对象 config = configparser.ConfigParser() # 读取配置文件 config.read('config_gpu.ini') def read_config_gpu(): name = config['hardware']['gpu'] if len(name) == 0: print("list is empty!") data = [] return data data = name.split(',') return data def write_config_gpu(add_name): name = config['hardware']['gpu'] if len(name) == 0: print("ok") data = [] data.append(add_name) new_name = ''.join(data) config.set('hardware', 'gpu', new_name) # 写入配置文件 with open('config_gpu.ini', 'w') as configfile: config.write(configfile) return data = name.split(',') if add_name in data: data.remove(add_name) data.append(add_name) new_name = ','.join(data) config.set('hardware', 'gpu', new_name) # 写入配置文件 with open('config_gpu.ini', 'w') as configfile: config.write(configfile) def del_name_gpu(): name = config['hardware']['gpu'] if len(name) == 0: print("list is empty!") return data = name.split(',') print(len(data)) data.pop(0) new_name = ','.join(data) config.set('hardware', 'gpu', new_name) # 写入配置文件 with open('config_gpu.ini', 'w') as configfile: config.write(configfile) if __name__ == '__main__': gpus = read_config_gpu() # 检查每个 GPU 是否都在列表中 for gpu_id in range(1, 5): # 假设我们只有到 GPU4 gpu_str = f"GPU{gpu_id}" if gpu_str not in gpus: print(f"{gpu_str} is go") write_config_gpu(f"GPU{gpu_id}") break else: print(f"{gpu_str} is available")