gpt4 book ai didi

python - 在python中读取资源文件

转载 作者:太空宇宙 更新时间:2023-11-04 04:17:22 26 4
gpt4 key购买 nike

我是一名 Java 开发人员转为 Python 开发人员。 python中如何读取classpath资源文件

这是我的目录结构

.
├── 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~

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

记录.py

class 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 <module>
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'

最佳答案

我可以使用下面的代码读取 json

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
abs_path = sys.path[0]
base_name = os.path.dirname(abs_path)
resources_path = os.path.join(base_name, "resources/credentials.json")

self.db_credentials = json.loads(open(resources_path).read())
self.db_manager = DataBaseManager(self.db_credentials['username'],
self.db_credentials['password'],
self.db_credentials['port'])

关于python - 在python中读取资源文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55173115/

26 4 0
文章推荐: c - 错误 : making pointer from integer in C
文章推荐: javascript - 单击以展开
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com