python - Python错误地将文件扩展名添加到路径字符串
问题描述
我目前正在努力解决以下问题:
我的文件夹结构看起来像:
master
- resources
customFile.fmu
fileCallingFMU.py
在执行fileCallingFMU.py 时,我传递了一个路径字符串,例如
path = "./resources/customFile.fmu"
我的脚本包含一个超级函数,我在其中传递路径变量。但是每次我执行脚本时都会出现异常:
Exception has occurred: FileNotFoundError
[Errno 2] No such file or directory: b'2021-11-16_./resources/customFile.fmu.txt'
File "[projectFolder]\fileCallingFMU.py", line 219, in __init__
super().__init__(path, config, log_level)
File "[projectFolder]\fileCallingFMU.py", line 86, in <module>
env = gym.make(env_name)
我现在迫切的问题如下:
python为什么以及如何使用日期前缀和.txt作为文件扩展名来操作路径变量?!
希望有人能在这方面启发我...
编辑
我正在尝试运行ModelicaGym的示例。
我的 fileCallingFMU.py 包含以下代码:
path = "./resources/customFile.fmu"
env_entry_point = 'cart_pole_env:JModelicaCSCartPoleEnv'
config = {
'path': path,
'm_cart': m_cart,
'm_pole': m_pole,
'theta_0': theta_0,
'theta_dot_0': theta_dot_0,
'time_step': time_step,
'positive_reward': positive_reward,
'negative_reward': negative_reward,
'force': force,
'log_level': log_level
}
from gym.envs.registration import register
env_name = env_name
register(
id=env_name,
entry_point=env_entry_point,
kwargs=config
)
env = gym.make(env_name)
entryPoint 的完整代码可以在这里找到。
解决方案
正如jjramsey指出的那样,问题隐藏在 ModelicaGym 库中。
记录器无法创建适当的日志文件,因为模型名称未正确存储在 self.model 变量中。
此错误的来源在于该行
self.model_name = model_path.split(os.path.sep)[-1]
由于 os 库无法分离我的路径字符串
"./resources/customFile.fmu"
改成之后
".\\resources\\customFile.fmu"
一切都按预期工作。
再次感谢!
推荐阅读
- http - 使用 ArduinoHTTPClient 通过 ESP32 发送 GET 请求时出现问题
- mysql - MySQL JSON 查询错误“字符串中缺少右引号”
- javascript - 如何在 vue3 上延迟加载图像?
- sql - 计算用户在某个日期之后给定时间范围内的记录
- airflow-scheduler - 气流:如何仅在所有重试失败时发送电子邮件警报
- ruby-on-rails - 使用涉及使用 ActiveRecord 连接表的 where 子句选择模型
- css - Tailwind CSS边框问题
- javascript - 点击时显示和隐藏不同的信息
- indexing - 谷歌搜索和我的网页有问题
- python - 使用登录名和每个文件的多个子路由从 Web 自动下载文件