ich verwende os.scandir
für das Erhalten der Liste der Dateien in einem Ordner:Zugang erstes Element in einem os.scandir
img_list2 = os.scandir('/home/shared/test')
Ich mag das erste Element erhalten.
Ich versuche img_list2.next()
>>> img_list2.next()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'posix.ScandirIterator' object has no attribute 'next'
ich also versucht:
>>> filt = list(img_list2)
>>> type(filt)
<class 'list'>
>>> globals()['filt']
[<DirEntry 'panaroma00010.jpg'>, <DirEntry 'panaroma00014.jpg'>, <DirEntry 'panaroma00004.jpg'>, <DirEntry 'panaroma00013.jpg'>, <DirEntry 'panaroma00007.jpg'>, <DirEntry 'panaroma00011.jpg'>, <DirEntry 'panaroma00012.jpg'>, <DirEntry 'panaroma00006.jpg'>, <DirEntry 'panaroma00009.jpg'>, <DirEntry 'panaroma00001.jpg'>, <DirEntry 'panaroma00003.jpg'>, <DirEntry 'panaroma00005.jpg'>, <DirEntry 'panaroma00002.jpg'>, <DirEntry 'panaroma00008.jpg'>]
>>> filt[1]
<DirEntry 'panaroma00014.jpg'>
>>>
So wie in der Liste wir das erste Element Wert bekommen, können wir es von os.scandir
bekommen?
Zusätzlich zu den Antworten finden Sie hier die Dokumentation für Iterator in Python 3: https://docs.python.org/3/library/stdtypes.html#iterator-types. Es besagt, dass Iteratoren nur die "magischen" Funktionen '__iter__' und' __next__' haben. –