1

In Unix we have a SOURCE command that can load a text file inside the shell script and all those parameters inside the text file is available as variables inside the script. Do we have an equivalent to that in python ? I need to load a properties file from the edge node to the .py script and based on the values take decisions inside the .py script.

Sample job.properties file in edge node is below.
databaseName=employee
hdfspath=/tenancy/ida/data
....

I need to load this job.properties file inside the .py script so that i need not pass these as command line arguments. Kindly advise

1
  • Make use of the import mechanism, I'd have thought. Commented Nov 6, 2019 at 12:09

2 Answers 2

1

You can in fact customize the import mechanism for such purposes. However, let me provide a quick hack instead:

def source(filename):
    variables = {}
    with open(filename) as f:
        for line in f:
            try:
                name, value = line.strip().split('=')
            except:
                continue
            variables[name] = value
    return variables

variables = source('job.properties')
print(variables)

The function source iterates over the lines in the supplied file and store the assignments in the dictionary variables. In case of lines not containing assignment, the try/except will simply skip it.

To emulate the behavior of shell sourcing further, you might add

globals().update(variables)

if working at the module (non-function) level, which will make databaseName and hdfspath available as Python variables.

Note that all "sourced" variables will be strs, even for lines such as my_int=42 in the sourced file.

Sign up to request clarification or add additional context in comments.

Comments

1

You could use 'exec' to achieve something similar to 'source'. It is not a nice thing to do.

It would look like:

with open("variables.py") as fi:
    exec(fi.read())

Easiest option is to use configparser module for ini files. If that is not desirable, try modules like yaml or json.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.