how to "source" file into python script

55,366

Solution 1

You could use execfile:

execfile("/etc/default/foo")

But please be aware that this will evaluate the contents of the file as is into your program source. It is potential security hazard unless you can fully trust the source.

It also means that the file needs to be valid python syntax (your given example file is).

Solution 2

Same answer as @jil however, that answer is specific to some historical version of Python.

In modern Python (3.x):

exec(open('filename').read())

replaces execfile('filename') from 2.x

Solution 3

If you know for certain that it only contains VAR="QUOTED STRING" style variables, like this:

FOO="some value"

Then you can just do this:

>>> with open('foo.sysconfig') as fd:
...   exec(fd.read())

Which gets you:

>>> FOO
'some value'

(This is effectively the same thing as the execfile() solution suggested in the other answer.)

This method has substantial security implications; if instead of FOO="some value" your file contained:

os.system("rm -rf /")

Then you would be In Trouble.

Alternatively, you can do this:

>>> with open('foo.sysconfig') as fd:
...   settings = {var: shlex.split(value) for var, value in [line.split('=', 1) for line in fd]}

Which gets you a dictionary settings that has:

>>> settings
{'FOO': ['some value']}

That settings = {...} line is using a dictionary comprehension. You could accomplish the same thing in a few more lines with a for loop and so forth.

And of course if the file contains shell-style variable expansion like ${somevar:-value_if_not_set} then this isn't going to work (unless you write your very own shell style variable parser).

Solution 4

Keep in mind that if you have a "text" file with this content that has a .py as the file extension, you can always do:

import mytextfile

print(mytestfile.FOO)

Of course, this assumes that the text file is syntactically correct as far as Python is concerned. On a project I worked on we did something similar to this. Turned some text files into Python files. Wacky but maybe worth consideration.

Solution 5

Just to give a different approach, note that if your original file is setup as

export FOO=/path/to/foo

You can do source /etc/default/foo; python myprogram.py (or . /etc/default/foo; python myprogram.py) and within myprogram.py all the values that were exported in the sourced' file are visible in os.environ, e.g

import os
os.environ["FOO"] 
Share:
55,366
Martin Vegter
Author by

Martin Vegter

Updated on January 03, 2020

Comments

  • Martin Vegter
    Martin Vegter over 4 years

    I have a text file /etc/default/foo which contains one line:

    FOO="/path/to/foo"
    

    In my python script, I need to reference the variable FOO.

    What is the simplest way to "source" the file /etc/default/foo into my python script, same as I would do in bash?

    . /etc/default/foo