diff options
Diffstat (limited to 'django/forms/fields.py')
-rw-r--r-- | django/forms/fields.py | 22 |
1 files changed, 11 insertions, 11 deletions
diff --git a/django/forms/fields.py b/django/forms/fields.py index 0f1a352cdc..9cc93a46e1 100644 --- a/django/forms/fields.py +++ b/django/forms/fields.py @@ -5,6 +5,7 @@ Field classes. import copy import datetime import math +import operator import os import re import uuid @@ -1104,17 +1105,16 @@ class FilePathField(ChoiceField): f = os.path.join(root, f) self.choices.append((f, f.replace(path, "", 1))) else: - try: - for f in sorted(os.listdir(self.path)): - if f == '__pycache__': - continue - full_file = os.path.join(self.path, f) - if (((self.allow_files and os.path.isfile(full_file)) or - (self.allow_folders and os.path.isdir(full_file))) and - (self.match is None or self.match_re.search(f))): - self.choices.append((full_file, f)) - except OSError: - pass + choices = [] + for f in os.scandir(self.path): + if f.name == '__pycache__': + continue + if (((self.allow_files and f.is_file()) or + (self.allow_folders and f.is_dir())) and + (self.match is None or self.match_re.search(f.name))): + choices.append((f.path, f.name)) + choices.sort(key=operator.itemgetter(1)) + self.choices.extend(choices) self.widget.choices = self.choices |