henkidefix
henkidefix

Reputation: 139

error with pymtp to work on python 3

I want to access a android device from python to download some photos. libmtp works from the CLI. Than pymtp. It's been around for a while but it's designed for python 2 and i'm using python 3. Meanwhile fixed several minor issues but i'm stuck at an error from function get_filelisting specially this section:

ret = []
next = files
while next:
    ret.append(next.contents)
    if (next(next.contents) is None):
        break
    next = next(next.contents)

The error is related to the "next".

That section looks strange to me, i've been coding in python for a while but i'm new to ctypes. Tried a lot of variants, they all failed. The "next" could be confusing with python buildin function so i renamed it to nextpointer and came to this code:

ret = []
nextpointer = files
while nextpointer:
    ret.append(nextpointer.contents)
    nextpointer = nextpointer.contents.next

It seems to work but did it work by accident ? does it have any design flaws ? Could anyone with experience on python ctypes confirm this a solution ? Any suggestion welcome.

Upvotes: 1

Views: 436

Answers (1)

zufyan
zufyan

Reputation: 11

From python2.7 documentation

next(iterator[, default])

Retrieve the next item from the iterator by calling its next() method. If default is given, it is returned if the iterator is exhausted, otherwise StopIteration is raised.

from python3 documentation

next(iterator[, default])

Retrieve the next item from the iterator by calling its __next__() method. If default is given, it is returned if the iterator is exhausted, otherwise StopIteration is raised.

Notice that next() method was removed from python3 but the function still exists.

This is all I can say about the next function and .next()/__next__() methods.

I downloaded the pymtp module and get_filelisting() is slightly different from what you posted in your ported code, here it is:

ret = []
next = files
while next:
    ret.append(next.contents)
    if (next.contents.next == None):
        break
    next = next.contents.next

If none of this helped you (which probably didn't :D), the version of pymtp library that I am using is 0.0.6 download using pip.

Upvotes: 1

Related Questions