python 资源文件_在python中读取资源文件

我是一个java开发人员转为python开发人员。如何在python中读取类路径资源文件

这是我的目录结构.

├── resources

│ ├── #test_schema.xml#

│ ├── create_confd_serialized_objects.sql

│ ├── create_notifications.txt

│ ├── create_notifications2.txt

│ ├── credentials.json

│ ├── delete_notifications.txt

│ ├── ngena-sa.xml

│ ├── ngena-sa.yang

│ ├── ngena-sa.yang~

│ ├── ngena-sa_v0.6.uml

│ ├── notification.txt

│ ├── notification.txt~

│ ├── requirements.txt

│ ├── test_schema.xml

│ └── test_schema.xml~

├── src

│ ├── ConfdAlertHandler.py

│ ├── ConfdAlertHandler.pyc

│ ├── ConfdAlertHandler.py~

│ ├── DataBaseManager.py

│ ├── DataBaseManager.pyc

│ ├── DataBaseManager.py~

│ ├── OUTPUT

│ ├── Record.py

│ ├── Record.py~

│ ├── __init__.py

│ ├── __pycache__

│ │ ├── ConfdAlertHandler.cpython-37.pyc

│ │ ├── DataBaseManager.cpython-37.pyc

│ │ └── socket.cpython-37.pyc

│ ├── listener.py

│ ├── ngena_sa_create.sql

│ ├── ngena_sa_create.sql~

│ ├── output.xml

│ ├── server.py

│ ├── server.py~

│ ├── watcher.py

│ └── watcher.py~

目前我们正在加载文件如下。是否有更好的方法或最佳实践来读取资源文件。

Record.pyclass Record:

def __init__(self, yang_path=None, json_path=None, xml_path=None,jsonData=None, xmlStr=None):

self.xmlStr = xmlStr

self.yang_path = yang_path

self.xml_path = xml_path

self.json_path = json_path

self.tables = []

self.module_name = ''

self.connections = []

self.table = None

self.db_credentials = json.loads(open("../resources/credentials.json").read())

self.db_manager = DataBaseManager(self.db_credentials['username'],

self.db_credentials['password'],

self.db_credentials['port'])

但是,我收到以下错误python src/Record.py resources/ngena-sa.yang

resources/ngena-sa.yang

Traceback (most recent call last):

File "src/Record.py", line 242, in

x = Record(args.yang_path)

File "src/Record.py", line 39, in __init__

self.db_credentials = json.loads(open("../resources/credentials.json").read())

FileNotFoundError: [Errno 2] No such file or directory: '../resources/credentials.json'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值