Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pipe output from shell command to a python script

Tags:

python

unix

I want to run a mysql command and set the output of that to be a variable in my python script.

Here is the shell command I'm trying to run:

$ mysql my_database --html -e "select * from limbs" | ./script.py 

Here is the python script:

#!/usr/bin/env python  import sys  def hello(variable):     print variable 

How would I accept the variable in the python script and have it print the output?

like image 809
David542 Avatar asked Jun 19 '12 21:06

David542


People also ask

How do you execute a shell command in Python and get output?

Get output from shell command using subprocess A better way to get the output from executing a linux command in Python is to use Python module “subprocess”. Here is an example of using “subprocess” to count the number of lines in a file using “wc -l” linux command.

Can you pipe in Python?

Pipe is a Python library that enables you to use pipes in Python. A pipe ( | ) passes the results of one method to another method. I like Pipe because it makes my code look cleaner when applying multiple methods to a Python iterable. Since Pipe only provides a few methods, it is also very easy to learn Pipe.

What command is used to output text from both the Python shell and within a Python module?

print() is the command you are looking for. The print() function, formally print statement in Python 2.0, can be used to output text from both the python shell and within a python module.


2 Answers

You need to read from stdin to retrieve the data in the python script e.g.

#!/usr/bin/env python  import sys  def hello(variable):     print variable  data = sys.stdin.read() hello(data) 

If all you want to do here is grab some data from a mysql database and then manipulate it with Python I would skip piping it into the script and just use the Python MySql module to do the SQL query.

like image 59
Jon Cage Avatar answered Sep 18 '22 15:09

Jon Cage


If you want your script to behave like many unix command line tools and accept a pipe or a filename as first argument, you can use the following:

#!/usr/bin/env python import sys  # use stdin if it's full                                                         if not sys.stdin.isatty():     input_stream = sys.stdin  # otherwise, read the given filename                                             else:     try:         input_filename = sys.argv[1]     except IndexError:         message = 'need filename as first argument if stdin is not full'         raise IndexError(message)     else:         input_stream = open(input_filename, 'rU')  for line in input_stream:     print(line) # do something useful with each line 
like image 37
mstringer Avatar answered Sep 18 '22 15:09

mstringer