Node = namedtuple('Node', ['name', 'type', 'size', 'datemodified', 'weight'])
class Core:
def __init__(self):
self.rootPath = '/fileuploader/rootdir'
self.currientPath = self.rootPath
def ScanDir(self):
files = []
with os.scandir(self.currientPath) as it:
for entry in it:
if entry.is_dir():
files.append(Node(
entry.name,
'folder',
0,
datetime.fromtimestamp(entry.stat().st_mtime),
0)
)
else:
files.append(Node(
entry.name,
str(mimetypes.guess_type(entry.path)[0]),
entry.stat().st_size,
datetime.fromtimestamp(entry.stat().st_mtime),
len(str(mimetypes.guess_type(entry.path)[0]))
))
files.sort(key=attrgetter('weight'))
return files
>>> print([entry for entry in os.scandir('/fileuploader/rootdir')])
[<DirEntry 'Soft'>, <DirEntry 'Games'>, <DirEntry 'Video'>, <DirEntry 'Books'>, <DirEntry 'Music'>]
datetime.fromtimestamp(entry.stat().st_mtime)
>>> [print(entry.stat()) for entry in os.scandir('/fileuploader/rootdir')]
os.stat_result(st_mode=16877, st_ino=14942408, st_dev=2052, st_nlink=2, st_uid=1000, st_gid=100, st_size=4096, st_atime=1515497744, st_mtime=1512115670, st_ctime=1512115670)
os.stat_result(st_mode=16877, st_ino=15598763, st_dev=2052, st_nlink=5, st_uid=487, st_gid=486, st_size=4096, st_atime=1515497739, st_mtime=1511698768, st_ctime=1511698768)
os.stat_result(st_mode=16893, st_ino=14942343, st_dev=2052, st_nlink=6, st_uid=1000, st_gid=100, st_size=4096, st_atime=1515497745, st_mtime=1514188702, st_ctime=1514188702)
Traceback (most recent call last):
File "<debugger>", line 1, in <module>
[print(entry.stat()) for entry in os.scandir('/fileuploader/rootdir')]
File "<debugger>", line 1, in <listcomp>
[print(entry.stat()) for entry in os.scandir('/fileuploader/rootdir')]
FileNotFoundError: [Errno 2] No such file or directory: '/fileuploader/rootdir/Books'