1

I am using os.scandir for getting the list of files in a folder:

img_list2 = os.scandir('/home/shared/test')

I want to get the first element.

I am trying 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'

So I tried:

>>> 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 just like in list we can get the first element value, can we get it from os.scandir?

Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
Santhosh
  • 9,965
  • 20
  • 103
  • 243
  • In addition to the answers below, here is the docs for Iterator in Python 3: https://docs.python.org/3/library/stdtypes.html#iterator-types. It says that Iterators only have the 'magic' functions `__iter__` and `__next__`. – Hubert Grzeskowiak Sep 13 '16 at 14:19

2 Answers2

6

os.scandir() returns an iterator containing the directory entries for a given path, see help(os.scandir):

scandir(...)
    scandir(path='.') -> iterator of DirEntry objects for given path

To get a value from it call the built-in next on it:

next(img_list2)

This will return values from the iterator until it is exhausted (and StopIteration is raised).

You can also wrap it in a list call which create a list out of all entries and make it indexable but if you only require the first element that is overkill.

iterator.next() was removed as of Python 3.0 with PEP 3114; a dunder iterator.__next__() method replaced it along with the built-in next() that calls it.

Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
  • 1
    how to make a list with indexable entries – Santhosh Sep 13 '16 at 17:44
  • you necessarily must pass `img_list2` in `list()` and then you can index it, i.e `img_list3 = list(img_list2)`. – Dimitris Fasarakis Hilliard Sep 13 '16 at 17:51
  • Also in jinja2 code of my django project, i tried to use next(img_list2).path . its says error `django.template.exceptions.TemplateSyntaxError: Could not parse the remainder: '(img_list2).path' from 'next(img_list2).path' [13/Sep/2016 23:21:38] "GET /gallery/ HTTP/1.1" 500 130929 ` – Santhosh Sep 13 '16 at 17:53
  • Calling `list` on the iterator will make it a list, `list` objects don't have a path attribute. `list(img_list2)[0].path` would work, as would `next(img_list2).path`. – Dimitris Fasarakis Hilliard Sep 13 '16 at 17:54
  • No i am sending the interator img_list2 to the template. `return render(request,'blog/gallery.html', {'img_list2':img_list2})` – Santhosh Sep 13 '16 at 17:56
  • `next(img_list2).path` is not working when used in the jinja code in my html template – Santhosh Sep 13 '16 at 17:57
  • I can't see the code so I can't reason as to why that happens; I'd suggest you ask another question. – Dimitris Fasarakis Hilliard Sep 13 '16 at 17:58
  • http://stackoverflow.com/questions/39476401/django-pythons-os-scandir-next-method-not-working-inside-jinja2-code (added as new question) – Santhosh Sep 13 '16 at 18:06
1

next() would give you the next item from the img_list2 iterator:

next(img_list2)
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195