擅长:python、mysql、java
<p>在大多数操作系统/文件系统中没有“创建时间”的实际跟踪:您得到的<code>mtime</code>是文件或目录被修改的时间(例如,在目录中创建文件会更新目录的mtime),而从<code>ctime</code>开始,如果提供了最新的inode更改时间(因此,可以通过创建或者删除子目录)。</p>
<p>假设您可以使用“last modified”(在问题中使用“created”只是一个错误),那么您可以找到(例如)当前目录的所有子目录:</p>
<pre><code>import os
all_subdirs = [d for d in os.listdir('.') if os.path.isdir(d)]
</code></pre>
<p>并获得最新的mtime(在Python 2.5或更高版本中):</p>
<pre><code>latest_subdir = max(all_subdirs, key=os.path.getmtime)
</code></pre>
<p>如果您需要在当前目录之外的其他地方操作,则不会有太大的不同,例如:</p>
<pre><code>def all_subdirs_of(b='.'):
result = []
for d in os.listdir(b):
bd = os.path.join(b, d)
if os.path.isdir(bd): result.append(bd)
return result
</code></pre>
<p>在给定的路径列表中,<code>latest_subdir</code>赋值不会改变
(无论是目录或文件的路径,<code>max</code>调用都会得到最新修改的路径)。</p>