<p>我是python编程新手。
我有这个问题:我有一个文本文件列表(压缩和非压缩),我需要:
-连接到服务器并打开它们
-打开文件后,我需要获取他的内容并将其传递给我编写的另一个python函数</p>
<pre><code>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 out
yield logInConst + curLine
def checkFile (regExp, fileName):
generatore = readLogs(fileName)
listOfMatches=[]
for i in generatore: #I'm now cycling through the logs
# regExp must be a COMPILE regular expression
if regExp.search(i):
listOfMatches.<a href="https://www.cnpython.com/list/append" class="inner-link">append</a>(i)
return listOfMatches
</code></pre>
<p>以便详细说明这些文件中包含的信息。
这个函数的目的是用三行代码将存储在这些文件中的日志写进一行。。。该函数对从本地计算机读取的文件运行良好,但我无法确定如何连接到远程服务器并创建这些单行日志,而不将每个文件的内容存储为字符串,然后使用字符串。。。我用来连接到远程计算机的命令是:</p>
<pre><code>connection_out = Popen(['ssh', retList[0], 'cd '+retList[2]+'; cat'+fileName], stdout=PIPE).communicate()[0]
</code></pre>
<p>retList[0]和retList[2]是用户@remote和我必须访问的文件夹名</p>
<p>提前感谢大家!</p>
<p><strong>更新:</strong></p>
<p>我的问题是,我必须先建立一个ssh连接:</p>
<pre><code>pr1=Popen(['ssh', 'siatc@lgssp101', '*~/XYZ/AAAAA/log_archive/00/MSG_090308_162648.gz*' ], stdout=PIPE).communicate()[0]
</code></pre>
<p>我需要打开的所有文件都存储在一个列表中,fileList[],其中一部分是压缩的(.gz),另一部分只是文本文件!!我已经试过了你在机器人出现之前的所有程序什么都没用。。。我想我应该修改Popen函数的第三个参数,但是我不知道怎么做!有人能帮我吗???</p>