-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_scanfiles.py
57 lines (48 loc) · 1.47 KB
/
utils_scanfiles.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# utils_files.py
try:
from os import scandir
except ImportError:
from scandir import scandir # use scandir PyPI module on Python < 3.5
import stat
import time
import os
import logging
logger = logging.getLogger(__name__)
def get_stat(entry):
time_format = "%Y-%m-%d %H:%M:%S"
f = {
"file":entry.name,
"folder":os.path.split(os.path.abspath(entry.path))[0],
"inode":entry.inode()
}
# get the stats
# os.stat_result(
# st_mode=33206,
# st_ino=0,
# st_dev=0,
# st_nlink=0,
# st_uid=0,
# st_gid=0,
# st_size=92702,
# st_atime=1543724526,
# st_mtime=1523310306,
# st_ctime=1523310474)
f["accessed"] = time.strftime(time_format,time.localtime(entry.stat().st_atime))
f["modified"] = time.strftime(time_format,time.localtime(entry.stat().st_mtime))
f["created"] = time.strftime(time_format,time.localtime(entry.stat().st_ctime))
f["size"] = entry.stat().st_size
f["mode"] = entry.stat().st_mode
return f
def scantree(path):
"""Recursively yield DirEntry objects for given directory."""
for entry in scandir(path):
try:
if entry.is_dir(follow_symlinks=False):
yield from scantree(entry.path) # see below for Python 2.x
else:
yield get_stat(entry)
except Exception as ex:
logger.error(ex)
if __name__ == "__main__":
for f in scantree("."):
print(f)