15

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

FooBar
  • 15,724
  • 19
  • 82
  • 171

5 Answers5

29

I guess you should use Path.iterdir().

for pth in dir_.iterdir():

    #Do your stuff here
Alex Shkop
  • 1,992
  • 12
  • 12
3

Try

for pth in dir_.iterdir():

Related documentation here: https://docs.python.org/3/library/pathlib.html#pathlib.Path.iterdir

Lee Thomas
  • 107
  • 1
  • 7
1

Use glob module instead, which works same on both platforms:

import glob
for item in glob.glob('/your/path/*')  # use any mask suitable for you
   print item # prints full file path
Gabriel M
  • 710
  • 4
  • 7
  • 3
    You can use the `.glob()` method on Path objects; no need to switch back to the legacy functions. – Jim Oldfield Aug 31 '16 at 14:56
  • In the past, I found the `glob.glob()` on Windows returns a list of filenames in lexical order, while on Linux order is not guaranteed. So when I ported code from Win to Linux, I had to sort the results from `glob.glob()` to have consistent behavior. I don't know if `Path.glob` is similar, but it is worth checking. – PaulMcG May 08 '17 at 15:57
1
dir_ = pathlib.Path(str(somePathVariable))
os.chdir(str(dir_))
for pth in dir_:        
    # some operations here

Now your code will work on both platforms. You are specifing de type of path... if you want it to be cross-platform you have to use "Path" not "PosixPath"

Frank Vieira
  • 45
  • 1
  • 6
0

Important to know: Every time you encounter an object is not iterable error, you must remember that the system also performs an iteration on a string, For example:

import yagmail

def send_email(to: list, subject: str, content: list, attachments=None):
    yagmail.SMTP(from_user_name, password)\
    .send(to=to, subject=subject, contents=content, attachments=attachments)

This function is also the contents of the email and the attachment must be in the list! (even if there is only one file).

In conclusion: Always try to insert the string into the list that can save a lot of problems.

Gavriel Cohen
  • 4,355
  • 34
  • 39