Run the cat command in a subprocess, Popen () python

If I execute the command below then python returns an excellent result.

result_aftermatch= subp.Popen('ls -lrt', stdout=subp.PIPE,stderr=subp.PIPE,shell=True)

but in the same way I have a requirement for greping lines from a code file as shown below ...

 list_of_id=[23,34,56,77,88] result_aftermatch= subp.Popen('egrep','list_of_IDs','/home/bimlesh/python/result.log', stdout=subp.PIPE,stderr=subp.PIPE,shell=True) result_lines,result_err= result_aftermatch.communicate() print result_lines 

The above code gives an error as shown below ...

 Traceback (most recent call last): File "test.py", line 144, in <module> result_aftermatch= subp.Popen('egrep','list_of_IDs','/home/bimlesh/python/result.log', stdout=subp.PIPE,stderr=subp.PIPE,shell=True) File "/usr/lib/python2.6/subprocess.py", line 573, in __init__ raise TypeError("bufsize must be an integer") TypeError: bufsize must be an integer 

Please, help.

+6
source share
2 answers

The problem is that you are passing the command as multiple arguments. You need to pass them as a list or tuple.

how

 subp.Popen([ 'egrep','list_of_IDs','/home/bimlesh/python/result.log' ], stdout=subp.PIPE,stderr=subp.PIPE,shell=True) 
+3
source

I assume you are looking for this:

 list_of_id = [23,34,56,77,88] ids_regex = '|'.join([str(i) for i in list_of_id]) result_aftermatch = subp.Popen(['egrep', ids_regex, '/home/bimlesh/python/result.log'], stdout=subp.PIPE, stderr=subp.PIPE) result_lines, result_err = result_aftermatch.communicate() print result_lines 
0
source

Source: https://habr.com/ru/post/946987/


All Articles