You are assuming that if it's not a file that it must be a directory. But that's not the case. It might not exist at all. You've only given the filename to Path, not the entire path. So it's not looking in
Change line 4 to something like
An alternative that is similar:
mydir
for the files, it's looking in your current directory. When it's not found, is_file() returns False.Change line 4 to something like
dr = pathlib.Path(mydir, esb)
An alternative that is similar:
import pathlib import os mydir = 'C:\\02' for file_object in pathlib.Path(mydir).iterdir(): if file_object.is_dir(): print(f"{file_object} is a Directory") elif file_object.is_file(): print(f"{file_object} is a File") else: print(f"{file_object} is neither file nor directory.")