我正在尝试使用脚本来查找包含文本字符串的文件。这些文件位于远程主机上,但是当我运行它时,我收到错误
回溯(最近一次调用最后一次):文件“dirsearch.py”,第55行,in fo = open(search_path + fname)IOError:[Errno 2]没有这样的文件或目录:u'/ home / black / white / goto / reports / dbs-01-Apr-2017.log'
我使用的脚本如下。
from __future__ import unicode_literals
from __future__ import division
from __future__ import absolute_import
from builtins import input
from builtins import open
from future import standard_library
standard_library.install_aliases()
import pysftp
#connect to sftp server
srv = pysftp.Connection(host="host", username="username",
password="password")
#acess remote directory on server
search_path = ('/home/black/white/goto/reports/')
file_type = '.log'
search_str = input("Enter the search string : ")
#addition ........or fname in os.listdir(path=search_path):
for fname in srv.listdir(search_path):
# Apply file type filter
if fname.endswith(file_type):
# Open file for reading
fo = open(search_path + fname)
# Read the first line from the file
line = fo.readline()
# Initialize counter for line number
line_no = 1
# Loop until EOF
while line != '' :
# Search for string in line
index = line.find(search_str)
if ( index != -1) :
print(fname, "[", line_no, ",", index, "] ", line, sep="")
# Read next line
line = fo.readline()
# Increment line counter
line_no += 1
# Close the files
fo.close()
srv.close()