We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent f1b5907 commit d6c7900Copy full SHA for d6c7900
torchvision/datasets/folder.py
@@ -4,6 +4,7 @@
4
5
import os
6
import os.path
7
+import sys
8
9
10
def has_file_allowed_extension(filename, extensions):
@@ -110,7 +111,11 @@ def _find_classes(self, dir):
110
111
Ensures:
112
No class is a subdirectory of another.
113
"""
- classes = [d for d in os.listdir(dir) if os.path.isdir(os.path.join(dir, d))]
114
+ if sys.version_info >= (3, 5):
115
+ # Faster and available in Python 3.5 and above
116
+ classes = [d.name for d in os.scandir(dir) if d.is_dir()]
117
+ else:
118
+ classes = [d for d in os.listdir(dir) if os.path.isdir(os.path.join(dir, d))]
119
classes.sort()
120
class_to_idx = {classes[i]: i for i in range(len(classes))}
121
return classes, class_to_idx
0 commit comments