Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirect bash output to python script

Tags:

python

bash

I am using zbarimg to scan bar codes, I want to redirect the output to a python script. How can I redirect the output of the following command:

zbarimg code.png

to a python script, and what should be the script like?

I tried the following script:

#!/usr/local/bin/python
s = raw_input()
print s

I made it an executable by issuing the following:

chmod +x in.py

Than I ran the following :

zbarimg code.png | in.py

I know it's wrong but I can't figure out anything else!

like image 983
Kartik Anand Avatar asked Apr 10 '13 15:04

Kartik Anand


2 Answers

Use sys.stdin to read from stdin in your python script. For example:

import sys
data = sys.stdin.readlines()
like image 163
dogbane Avatar answered Sep 22 '22 07:09

dogbane


Using the pipe operator | from the command is correct, actually. Did it not work?

You might need to explicitly specify the path for the python script as in

zbarimg code.png | ./in.py

and as @dogbane says, reading from stdin like sys.stdin.readlines() is better than using raw_input

like image 28
Leopd Avatar answered Sep 21 '22 07:09

Leopd