Python - 脚本执行、定位错误
我尝试使用 ConfigParser 模块读取配置文件,以便可以安全地允许用户编辑应用程序的设置并在脚本之间共享这些配置。 这对于主脚本来说非常有效。但是,有一个辅助脚本被调用,它在同一位置读取相同的配置文件,但返回一个错误,指出找不到该位置。 两个脚本都位于同一目录中,application/bin/
配置文件位于 application/conf/
为了在主脚本中成功引用配置文件,我使用了以下完美运行的代码。
config = ConfigParser.ConfigParser()
config.readfp(open('../conf/settings.conf'))
当辅助脚本使用相同的代码执行时,它报告该位置不存在?我使用了 logger 模块并让它记录 sys.path[0] ,它正确返回与主脚本相同的 bin 文件夹。我在这里可能缺少一些简单的东西吗?
另外,欢迎提供针对此类问题的任何故障排除提示。
Using the ConfigParser module I am attempting to read in a configuration file so I can safely allow users to edit settings for an application and share these configurations between scripts.
This has been working perfectly for the main script. However there is a secondary script that is called and it reads the same configuration file in the same location but returns an error that the location cannot be found.
Both scripts are located within the same directory, application/bin/
The configuration file is located in application/conf/
To reference the config file successfully in the main script I use the following code which works perfectly.
config = ConfigParser.ConfigParser()
config.readfp(open('../conf/settings.conf'))
When the secondary script executes with the same code it reports that the location does not exist? I used the logger module and got it to log sys.path[0] which correctly returned the same bin folder as the main script. Is there possibly something simple I am missing here?
Also any troubleshooting tips for problems like these are welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以更喜欢使用
dirname
和__file__
:具体取决于您启动应用程序的方式(python bin/app.py 或 python app.py),“.. ' 将是不正确的。通过从 .py 文件的目录开始,您始终能够使用该方法构建从 .py 到 .conf 的路径。
You can prefer to use
dirname
and__file__
:Depending how you're launching the app (python bin/app.py, or python app.py), the '..' will be incorrect. By starting from the directory of the .py file, you'll always be able to construct the path from the .py to the .conf using that method.