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