IOError: [Errno 2] No such file or directory while using pysftp

719 views Asked by At

I am trying to do a script to find files that contain a text string. The files are on a remote host but when I run it, I get an error

Traceback (most recent call last): File "dirsearch.py", line 55, in fo = open(search_path + fname) IOError: [Errno 2] No such file or directory: u'/home/black/white/goto/reports/dbs-01-Apr-2017.log'

The script I used is below.

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()
0

There are 0 answers