I am trying to have an automated script that enters into the most recently created folder.
I have some code below
import datetime, os, shutil
today = datetime.datetime.now().isoformat()
file_time = datetime.datetime.fromtimestamp(os.path.getmtime('/folders*'))
if file_time < today:
changedirectory('/folders*')
I am not sure how to get this to check the latest timestamp from now. Any ideas?
Thanks
There is no actual trace of the “time created” in most OS / filesystems: what you get as
mtimeis the time a file or directory was modified (so for example creating a file in a directory updates the directory’s mtime) — and fromctime, when offered, the time of the latest inode change (so it would be updated by creating or removing a sub-directory).Assuming you’re fine with e.g. “last-modified” (and your use of “created” in the question was just an error), you can find (e.g.) all subdirectories of the current directory:
and get the one with the latest mtime (in Python 2.5 or better):
If you need to operate elsewhere than the current directory, it’s not very different, e.g.:
the
latest_subdirassignment does not change given, asall_subdirs, any list of paths(be they paths of directories or files, that
maxcall gets the latest-modified one).