I'm new to python programming.
I have this problem: I have a list of text files (both compressed and not) and I need to :
- connect to the server and open them
- after the opening of the file, I need to take his content and pass it to another python function that I wrote
def readLogs (fileName):
f = open (fileName, 'r')
inStream = f.read()
counter = 0
inStream = re.split('\n', inStream) # Create a 'list of lines'
out = "" # Will contain the output
logInConst = "" # log In Construction
curLine = "" # Line that I am working on
for nextLine in inStream:
logInConst += curLine
curLine = nextLine
# check if it is a start of a new log && check if the previous log is 'ready'
if newLogRegExp.match(curLine) and logInConst != "":
counter = counter + 1
out = logInConst
logInConst = ""
yield logInConst + curLine
def checkFile (regExp, fileName):
generatore = readLogs(fileName)
for i in generatore: #I'm now cycling through the logs
# regExp must be a COMPILE regular expression
connection_out = Popen(['ssh', retList, 'cd '+retList+'; cat'+fileName], stdout=PIPE).communicate()
pr1=Popen(['ssh', 'siatc@lgssp101', '*~/XYZ/AAAAA/log_archive/00/MSG_090308_162648.gz*' ], stdout=PIPE).communicate()
You do not have to split the stream/file into lines yourself. Just iterate:
for ln in f: # work on line in ln
This should work equally well for files (using open() for file()) and pipes (using Popen). Use the
stdout property of the popen object to access the pipe connected to stdout of the subprocess
from subprocess import Popen, PIPE pp = Popen('dir', shell=True, stdout=PIPE) for ln in pp.stdout: print '#',ln