Python – Iterate through files

pathlib, python

I'm trying to adapt someone's code for my (Windows 7) purposes. His is unfortunately UNIX specific. He does

dir_ = pathlib.PosixPath(str(somePathVariable))os.chdir(str(dir_))for pth in dir_:            # some operations here

Running this, I got (not surprisingly)

NotImplementedError: cannot instantiate 'PosixPath' on your system

I looked into the documentation for pathlib and thought yeah, I should just be able to change PosixPath to Path and I would be fine. Well, then dir_ generates a WindowsPath object. So far, so good. However, I get

TypeError: 'WindowsPath' object is not iterable

pathlib is at version 1.0, what am I missing? The purpose is to iterate through files in the specific directory. Googling this second error gave 0 hits.

Remark: Could not use pathlib as a tag, hence I put it into the title.

Update

I have Python 2.7.3 and pathlib 1.0

Best Solution

I guess you should use Path.iterdir().

for pth in dir_.iterdir():    #Do your stuff here