cached_folder = os.path.expandvars(r'%APPDATA%\Microsoft\Windows\Themes\CachedFiles\*')
list_of_files = glob.glob(cached_folder)
latest_file = max(list_of_files, key=os.path.getctime)
我只想要CachedFiles中最新文件的路径,尽管这不起作用,并且
list_of_files
是一个空列表。我在这里做错了什么?
最佳答案
下面的代码(基本上包装了您的代码)对我有用。
您可能有空%APPDATA%\Microsoft\Windows\Themes\CachedFiles
。
#!/usr/bin/env python
def main():
import os, glob
cached_folder = os.path.expandvars(r'%APPDATA%\Microsoft\Windows\Themes\CachedFiles\*')
list_of_files = glob.glob(cached_folder)
print( list_of_files )
latest_file = max(list_of_files, key=os.path.getctime)
print( latest_file )
return
if (__name__ == '__main__' ) :
main()
关于python - 如何从目录中获取最新文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61501473/