Data exchange between python script and bash script

Venkatachalam Srinivasan venkatachalam.19 at gmail.com
Tue Apr 4 13:39:08 EDT 2017


On Tuesday, April 4, 2017 at 5:42:23 PM UTC+2, justin walters wrote:
> On Tue, Apr 4, 2017 at 8:01 AM, <venkatachalam.19 at gmail.com> wrote:
> 
> > Hello All,
> >
> > I am writing a python code for processing a data obtained from a sensor.
> > The data from sensor is obtained by executing a python script. The data
> > obtained should be further given to another python module where the
> > received data is used for adjusting the location of an object.
> >
> > For achieving this, there is a central bash script, which runs both the
> > python modules parallel. Something like:
> >
> > python a.py &
> > python b.py &
> >
> > I am trying to return the sensor data to the bash .sh file, therefore it
> > can be provided to the other script. This, based on the online tutorials
> > looks like:
> >
> > sensor_data=$(python execute_sensor_process.py) &
> >
> > and the sensor_data is assigned by printing the required data in the
> > corresponding python script. For example, the data is printed in
> > execute_sensor_process.py as follows:
> >
> > print >>sys.stderr,sens_data
> >
> > By printing the data onto sys.stderr and assigning a return variable in
> > the bash, I am expecting the data to be assigned.
> >
> > But this is not happening. The sensor data is a dictionary and I like to
> > have this data for further analysis. I am not getting the data returned
> > from the python script on to the bash variable.
> >
> > Can someone help me to understand why the code is not working? I tried
> > other approaches of function call such as
> >
> >
> > sensor_data=$`python execute_sensor_process.py` &
> >
> >
> > python execute_sensor_process.py tempfile.txt &
> > kinexon_data=`cat tempfile.txt` &
> >
> > But none of the approaches are working.
> >
> > Thank you,
> > Venkatachalam Srinivasan
> > --
> > https://mail.python.org/mailman/listinfo/python-list
> >
> 
> I'm not sure why you need the data in a bash script. What can you do with a
> bash script that you
> can't do with Python?
> 
> That said, it seems to me that you need a data persistence layer, i.e. a
> way to store the dictionary
> after it is created. My best advice would be to use the json module to
> write out to json files. I
> believe this is the best approach because json has a near 1-to-1 mapping
> with Python
> dictionaries.
> 
> Json is also a good choice because almost every language has a json parser
> as part of it's standard
> library, making you data extremely portable.

Hi,

Thanks for the answer. I need bash for connecting data exchange between two python scripts. To be more specific, data from one script has to be passed to the another script. You are right, I don't need the data in the bash other than to pass the obtained data to the another script which uses the data for further analysis.

Regarding using the json file, I am new to this. A naive question is that if the data is too large then is json file is easy to handle? Is json file for large data is not computationally expensive? I am not using textfile for the same reason of being computationally expensive.

Thanks,
Venkatachalam Srinivasan


More information about the Python-list mailing list