Replies: 2 comments 1 reply
-
tl;drTo get the import os
atime, mtime, ctime = os.stat("/boot.py")[-3:] timestamp > datetime-objectThe existing micropython-datetime library has no support for On different platforms are different offsets used. This can be used to calculate the offset, but first you need the library. You can install the library if your controller is connected to a wifi and has an internet connection: import mip
mip.install("datetime") Example Codeimport datetime
import os
import sys
if "ESP32" in sys.implementation._machine:
OFFSET = datetime.datetime(2000, 1, 1, 0, 0)
else:
OFFSET = datetime.datetime(1970, 1, 1, 0, 0)
# offsets of other boards are not tested and missing
class FileNotExistsError(OSError):
pass
class StatsResult:
"""
Stats result of a file or directory.
"""
def __init__(self, file):
self.mode, *_, self.size, self.atime, self.mtime, self.ctime = os.stat(file)
self.file = file
@property
def is_directory(self) -> bool:
return bool(self.mode >> 14 & 1)
@property
def is_file(self) -> bool:
return bool(self.mode >> 15 & 1)
@property
def dt_atime(self):
return OFFSET + datetime.timedelta(seconds=self.atime)
@property
def dt_mtime(self):
return OFFSET + datetime.timedelta(seconds=self.mtime)
@property
def dt_ctime(self):
return OFFSET + datetime.timedelta(seconds=self.ctime)
def stats(file: str) -> StatsResult:
"""
Stats result of a file or directory.
"""
try:
return StatsResult(file)
except OSError as error:
if error.args[0] == 2:
raise FileNotExistsError(f"{file} does not exist.")
else:
raise |
Beta Was this translation helpful? Give feedback.
-
OMG this is as good as text book, not only a detailed explanation, but also a great example of code - readability. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Is there a way to get time of modification and/or creation on files in micropython?
many thanks.
Beta Was this translation helpful? Give feedback.
All reactions