summaryrefslogtreecommitdiff
path: root/django/forms
diff options
context:
space:
mode:
Diffstat (limited to 'django/forms')
-rw-r--r--django/forms/fields.py16
1 files changed, 9 insertions, 7 deletions
diff --git a/django/forms/fields.py b/django/forms/fields.py
index daa65d61eb..57886656de 100644
--- a/django/forms/fields.py
+++ b/django/forms/fields.py
@@ -1111,13 +1111,15 @@ class FilePathField(ChoiceField):
self.choices.append((f, f.replace(path, "", 1)))
else:
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))
+ with os.scandir(self.path) as entries:
+ for f in entries:
+ 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)