Skip to content
pvmehta.com

pvmehta.com

  • Home
  • About Me
  • Toggle search form
  • Parallel DML Oracle
  • How to find Unix server conguraiton including Server Model number and underlying disk sub-system ( On Solaris) Linux/Unix
  • Find average Row Length and other table size calculation. metalink notes Oracle
  • To check whether standby is recovering properly or not?? Oracle
  • longtx.sql Oracle
  • Search and Replace vi editor command. Linux/Unix
  • chk_space_SID.ksh Linux/Unix
  • Difference between SYNC and AFFIRM Oracle
  • Search and replace editor command in vi Linux/Unix
  • Check_recovery.sh program to run sqlplus and return its values remotely. Linux/Unix
  • Good RAC & Standby Notes Oracle
  • Wait time tuning research Oracle
  • Find total file sizes Linux/Unix
  • good linux notes Linux/Unix
  • Guide to Linux System Command Mastery Linux/Unix

Reading config file from other folder inside class

Posted on 24-Sep-2024 By Admin No Comments on Reading config file from other folder inside class

Directory Structure:

root-folder/
    config-folder/
        json-file.config
    class-folder/
        classfile.py
    tests/
        pytest-file1.py

json-file.config:

{
    "setting1": "value1",
    "setting2": "value2"
}

Now, Here I need to access json-file.config in my class file. I do not want to use dbutil, how can i access it and read it from json-file.config in my class file??

  • For this task, update contents of your class file as mentioned below.

classfile.py

import os
import json

class myclass:
    def __init__(self):
        # Get the path of the json-file.config
        config_file_path = os.path.join(os.path.dirname(__file__), '..', 'config-folder', 'json-file.config')

        # Read the JSON config file
        with open(config_file_path, 'r') as file:
            self.config = json.load(file)
    
    def get_setting(self, key):
        return self.config.get(key, "Setting not found")

Explanation:

os.path.join: Constructs the path to the json-file.config file relative to the location of classfile.py by going up one level (..) and then into the config-folder/.

json.load(file): Reads the content of the json-file.config and loads it into a dictionary.

Usage: You can now access the JSON config in your pytest-file1.py by creating an instance of myclass.

Example usage in pytest-file1.py:

from class_folder.classfile import myclass

def test_read_config():
    # Create an instance of the class
    obj = myclass()

    # Access a specific setting from the config
    setting_value = obj.get_setting('setting1')
    
    # Example assertion (for pytest)
    assert setting_value == "value1"
Python/PySpark

Post navigation

Previous Post: Python class import from different folders
Next Post: Exadata Basics

Related Posts

  • Read CSV File using Python Python/PySpark
  • Read CSV file using PySpark Python/PySpark
  • Getting started with notebook Python/PySpark
  • Python class import from different folders Python/PySpark
  • How to connect to Oracle Database with Wallet with Python. Oracle
  • Add new columns in dataframe Python/PySpark

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • Ansible (0)
  • AWS (2)
  • Azure (1)
  • Linux/Unix (149)
  • MYSQL (5)
  • Oracle (392)
  • PHP/MYSQL/Wordpress (10)
  • POSTGRESQL (0)
  • Power-BI (0)
  • Python/PySpark (7)
  • RAC (17)
  • rman-dataguard (26)
  • shell (149)
  • SQL scripts (341)
  • SQL Server (6)
  • Uncategorized (0)
  • Videos (0)

Recent Posts

  • SQL Server Vs Oracle Architecture difference25-Jul-2025
  • SQL Server: How to see historical transactions25-Jul-2025
  • SQL Server: How to see current transactions or requests25-Jul-2025
  • T-SQL Vs PL/SQL Syntax25-Jul-2025
  • Check SQL Server edition25-Jul-2025
  • Checking SQL Server Version25-Jul-2025
  • Oracle vs MYSQL Architecture differences (For DBAs)24-Jul-2025
  • V$INSTANCE of Oracle in MYSQL24-Jul-2025
  • Day to day MYSQL DBA operations (Compared with Oracle DBA)24-Jul-2025
  • MYSQL and Oracle Comparison for Oracle DBA24-Jul-2025

Archives

  • 2025
  • 2024
  • 2023
  • 2010
  • 2009
  • 2008
  • 2007
  • 2006
  • 2005
  • Order by with ROWNUM Oracle
  • kill all processes from specific user in solaris. Linux/Unix
  • My Test Case On 21-OCT-2005 Oracle
  • Generate SSH without password authentication. Linux/Unix
  • Oracle Recommended Patches — Oracle Database ID 756671.1 Oracle
  • Sending SQLPLUS output in HTML format Oracle
  • MYSQL for Oracle DBA MYSQL
  • age_alert.ksh aging out alert.log Linux/Unix

Copyright © 2025 pvmehta.com.

Powered by PressBook News WordPress theme