parse an environment file

Hans Mulder hansmu at xs4all.nl
Mon Oct 1 11:35:04 EDT 2012


On 1/10/12 16:12:50, Jason Friedman wrote:
>> I want my python 3.2.2 script, called via cron, to know what those
>> additional variables are.  How?
> 
> Thank you for the feedback.  A crontab line of
> 
> * * * * * . /path/to/export_file && /path/to/script.py
> 
> does indeed work, but for various reasons this approach will not
> always be available to me.
> 
> Let me restate my question.  I have a file that looks like this:
> export VAR1=foo
> export VAR2=bar
> # Comment
> export VAR3=${VAR1}${VAR2}
> 
> I want this:
> my_dict = {'VAR1': 'foo', 'VAR2': 'bar', 'VAR3': 'foobar'}
> 
> I can roll my own, but I'm thinking there is a module or existing code
> that does this.  I looked at the os and sys and configparse modules
> but did not see it.

One tactic is to write a wrapper script in shellese that sets the
variables and then runs your script.  Something like:

#/bin/bash
export VAR1=foo
export VAR2=bar
# Comment
export VAR3=${VAR1}${VAR2}

# Read some more settings from a file
. /path/to/file/with/more/exports

# Drum roll .....
/path/to/your/script.py


This allows you to copy-and-paste all sorts of weird and wonderful
shell syntax into your wrapper script.

AFAIK, there is no Python module that can read shell syntax.
You could translate all that shell syntax manually to Python,
but that may not be worth the effort.

Hope this helps,

-- HansM




More information about the Python-list mailing list