0

Sometimes I need to iterate through the files in a folder with pysftp and I want to retrieve information for the files matching with my fnmatch_pattern (refer to fnmatch). I would like that the function conn.stat("path") raised an error in case the file wasn't present.

Reproducible example below:

   def get_mod_time_pattern(self, dir_path: str, fnmatch_pattern: str = None) -> Dict[str, str]:
        d = {}
        conn = pysftp.Connection(**my_conn_paramt)
        
        # file list of files respecting matches
        flist = [file for file in conn.listdir(dir_path) if fnmatch(file, fnmatch_pattern)]

        for file in flist:
            path_to_file = os.path.join(dir_path, file)
            d[path_to_file] = conn.stat(path_to_file).st_mtime

    return d

with this solution - where flist is empty - the function conn.stat doesn't raise the usual IOError that is normally raised when the file is not existing. How can I make it raising the error? Thanks

Saverio Guzzo
  • 419
  • 5
  • 16
  • 1
    When `flist` is empty, your code will never even call the `conn.stat`. + Your code is wrong anyway. If you want file details, use `listdir_attr` – See https://stackoverflow.com/q/12295551/850848#57694519 – Martin Prikryl Aug 07 '21 at 19:03
  • yes, it did. I need to find a way in which `conn.stat` is called; thanks! – Saverio Guzzo Aug 12 '21 at 06:57

0 Answers0