1

I have a text file contains all the paths belong to bunch of commands that can be called in the bash scrpits and it is called progs.ini. Usually when I want to call this configuration file in my bash script I use this command

. progs.ini

progs.ini contains stuff for instance like this:

BIN=/bin/linux_64/
P_ANALYSE=${BIN}/analyse
NPARA=1

now I want to use some part of my code in python and I was trying to use this command as following:

import subprocess as S
import os
CMD='. progs.ini'
S.call([CMD],shell=True)

It doesn't return any error message but it can not recognise the variables which are defined in progs.ini

>>os.system('echo ${BIN}')

0

Well it is not about setting some environmental variable which is similar to this problem. I want to set some variables using the configuration file.

4
  • 1
    The shell processes run by subprocess and os.system are unrelated. They do not share any state. Commented Jul 15, 2015 at 13:20
  • @EtanReisner so then what is related to this problem? Commented Jul 15, 2015 at 13:21
  • 2
    No. I meant the are unrelated to each other. You have effectively just run bash -c '. progs.ini'; bash -c 'echo $BIN' and wondered why the variable didn't work. Those two commands don't share any state. They don't share variables, etc. They are entirely different processes. Commented Jul 15, 2015 at 13:23
  • @EtanReisner Is there any way that I can set these variables which are a huge pile of them in my python script without re-writting them? Commented Jul 15, 2015 at 13:30

1 Answer 1

2

You seem to be using Linux. In that case I would be inclined to put a cat /proc/$$/environ at the end of your ini file. That will print out all the key value pairs in a format that's easy to parse. This should do:

s = os.popen(". whatever.ini && cat /proc/$$/environ").read()
env_vars = {x[:x.find("=")]:x[x.find("=")+1:] for x in s.split("\00")[:-1]}

Tested. That didn't work but this did:

s = os.popen(". ./whatever.ini && set").read()
env_vars = {x[:x.find("=")]:x[x.find("=")+2:-1] for x in s.split("\n")[:-1]}
print env_vars['hello']
Sign up to request clarification or add additional context in comments.

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.