首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >在python中读取资源文件

在python中读取资源文件
EN

Stack Overflow用户
提问于 2019-03-15 06:45:41
回答 1查看 988关注 0票数 2

我是一个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.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'
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-15 07:05:36

我可以使用下面的代码阅读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'])
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55173115

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档