2013-06-10 3 views
6

की उपप्रक्रिया में बिल्ली आदेश पर अमल, popen() मैं आदेश से नीचे चल रहा हूँ तो अजगर महान परिणाम लौटने है ..अजगर

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

लेकिन उसी तरह से मैं फ़ाइल से greping लाइनों की आवश्यकता है कोड के साथ नीचे के रूप में है ...

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 

कोड से ऊपर नीचे के रूप में त्रुटि दे रहा है ...

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 

कृपया मदद करें।

उत्तर

3

समस्या यह है कि आप कमांड को एकाधिक तर्क के रूप में पास कर रहे हैं। आपको उन्हें एक सूची या टुपल के रूप में पास करने की आवश्यकता है।

तरह:

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

मैं नीचे दिए गए कोड की कोशिश की ... –

0

मैं तुम्हें अनुमान है कि इस के लिए देख रहे:

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

मैं ऊपर नहीं बल्कि कोशिश की प्रिंट में कुछ भी हो रही है (कोई लाइनों , कोई त्रुटि नहीं)। शैल के साथ आना = सही भी और त्रुटि प्राप्त करना "उपयोग: egrep [विकल्प] ... पैटर्न [फ़ाइल] ... अधिक जानकारी के लिए 'egrep --help' आज़माएं।" –

+0

प्रिय दर्शक और ब्लॉगर्स, फिर भी मुझे सही जवाब नहीं मिला। कृपया मदद करें। –