diff --git a/Lib/pathlib.py b/Lib/pathlib.py index 6be372ed320a2c..648194035ce820 100644 --- a/Lib/pathlib.py +++ b/Lib/pathlib.py @@ -1060,9 +1060,14 @@ def iterdir(self): """Iterate over the files in this directory. Does not yield any result for the special paths '.' and '..'. """ + #Below line throws FileNotFoundError if path is invalid + dirs = self._accessor.listdir(self) + return self._iterdirgen(dirs) + + def _iterdirgen(self,dirs): if self._closed: self._raise_closed() - for name in self._accessor.listdir(self): + for name in dirs: if name in {'.', '..'}: # Yielding a path object for these makes little sense continue diff --git a/Misc/NEWS.d/next/Library/2018-08-29-19-20-20.bpo-34541.prMad02.rst b/Misc/NEWS.d/next/Library/2018-08-29-19-20-20.bpo-34541.prMad02.rst new file mode 100644 index 00000000000000..08cefb2b8b546a --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-08-29-19-20-20.bpo-34541.prMad02.rst @@ -0,0 +1,2 @@ +Added code to make pathlib.Path.iterdir throw FileNotFoundError when path is + not valid \ No newline at end of file