Python calls the Linux Shell script and returns a result example.
Last Update:2018-12-07
Source: Internet
Author: User
#! /Usr/bin/Python #-*-coding: cp936-*-import re; import OS; import sys; if (_ name __= = "_ main _"): P = Re. compile ('(^ \ s + | \ s + $)'); pdigit = Re. compile ('^ \ D +'); delimiter = '\ T'; strdict = 'dict. dat '; strcontigency = 'contigency. dat '; str_input = STR (sys. argv [1]); str_weight = STR (sys. argv [2]); str_output = STR (sys. argv [3]); fid_input = file (str_input, 'R'); fid_output = file (str_output, 'w'); KEYWORDS = []; keywordstmp = fid_input. Readlines (); For m in keywordstmp: mykey = P. sub ('', m); keywords. append (mykey); for keyword in keywords: Results = []; # results. append (keyword); command = ''; command = 'grep-N ^ % S $ % s' % (keyword, strdict); tmpcol = OS. popen (command ). readlines (); temp = P. sub ('', tmpcol [0]); temps = temp. split (':'); linenum = int (temps [0]) + 1; command = "sed-n' % DP '% s" % (linenum, strdict ); tmpcol = OS. popen (command ). readlines (); temp = P. Sub ('', tmpcol [0]); # indicate how?docs key indexed; totalindexed = temp; # results. append (temp); command = 'grep-N ^ % S $ % s' % (keyword, str_weight); tmpcol = OS. popen (command ). readlines (); temp = P. sub ('', tmpcol [0]); temps = temp. split (':'); linenum = int (temps [0]) + 2; command = "sed-n '% DP' % s" % (linenum, str_weight ); tmpcol = OS. popen (command ). readlines (); temp = P. sub ('', tmpcol [0]); temps = temp. split (''); myclas S = temps [0] # results. append (myclass); command = "grep-n '^ % S % s' % s" % (keyword, myclass, strcontigency); tmpcol = OS. popen (command ). readlines (); temp = P. sub ('', tmpcol [0]); temps = temp. split (':'); temp1 = P. sub ('', temps [1]); Results = temp1.split (''); results. append (totalindexed); # numin = m [1]; # numnotin = m [2]; # print numin; # print numnotin; # print results # S = raw_input ('Please enter'); # results. append (numin );# Results. append (numnotin); myline = delimiter. join (results); fid_output.write (myline); fid_output.write ('\ n'); print myline; fid_input.close (); fid_output.close (); print' % s has finished, congratulations! '% STR (SYS. argv [0]);