1
0
mirror of https://github.com/django/django.git synced 2024-12-22 17:16:24 +00:00

Fixed #29689 -- Improved performance of FileSystemStorage.listdir() and FilePathField with os.scandir().

This commit is contained in:
Federico Bond 2018-08-19 20:21:57 -03:00 committed by Tim Graham
parent 371ece2f06
commit a0ca4b5694
3 changed files with 19 additions and 19 deletions

View File

@ -309,11 +309,11 @@ class FileSystemStorage(Storage):
def listdir(self, path):
path = self.path(path)
directories, files = [], []
for entry in os.listdir(path):
if os.path.isdir(os.path.join(path, entry)):
directories.append(entry)
for entry in os.scandir(path):
if entry.is_dir():
directories.append(entry.name)
else:
files.append(entry)
files.append(entry.name)
return directories, files
def path(self, name):

View File

@ -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

View File

@ -39,11 +39,11 @@ class PathNotImplementedStorage(storage.Storage):
def listdir(self, path):
path = self._path(path)
directories, files = [], []
for entry in os.listdir(path):
if os.path.isdir(os.path.join(path, entry)):
directories.append(entry)
for entry in os.scandir(path):
if entry.is_dir():
directories.append(entry.name)
else:
files.append(entry)
files.append(entry.name)
return directories, files
def delete(self, name):