Skip to content

Commit

Permalink
Use scandir to save memory
Browse files Browse the repository at this point in the history
  • Loading branch information
QuantumEnergyE committed Dec 22, 2018
1 parent edba525 commit 43f05c2
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/watchdog/utils/dirsnapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
import os
from stat import S_ISDIR
from watchdog.utils import stat as default_stat
try:
from os import scandir as listdir
except ImportError:
from os import listdir


class DirectorySnapshotDiff(object):
Expand Down Expand Up @@ -199,7 +203,7 @@ class DirectorySnapshot(object):
def __init__(self, path, recursive=True,
walker_callback=(lambda p, s: None),
stat=default_stat,
listdir=os.listdir):
listdir=listdir):
self._stat_info = {}
self._inode_to_path = {}

Expand All @@ -209,7 +213,8 @@ def __init__(self, path, recursive=True,

def walk(root):
try:
paths = [os.path.join(root, name) for name in listdir(root)]
paths = [os.path.join(root, entry if isinstance(entry, str) else entry.name)
for entry in listdir(root)]
except OSError as e:
# Directory may have been deleted between finding it in the directory
# list of its parent and trying to delete its contents. If this
Expand Down

0 comments on commit 43f05c2

Please sign in to comment.