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?
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With