繁体   English   中英

按创建日期的顺序打印目录的内容

[英]Printing out contents of a directory in order of creation date

有没有人用python完成这个?

这是我到目前为止所拥有的......

if os.path.isdir(options.map_file_directory):
    searchedfile = glob.glob("*.map")
    files = sorted( searchedfile, key = lambda file: os.path.getctime(file))

    for i in files:
        print("{} - {}".format(i, time.ctime(os.path.getctime(i))) )

解决了我自己的问题。 与我的“全球化”方式有关

if os.path.isdir(options.map_file_directory):
    print ("this is a test 4")
    searchedfile = glob.glob(r'{}\*.map'.format(options.map_file_directory))
    files = sorted( searchedfile, key = lambda file: os.path.getctime(file))

    for i in files:
        print("{} - {}".format(i, time.ctime(os.path.getctime(i))) )

如果您不想只对时间值进行排序,而且还要将其打印出来,则可以将时间存储在要排序的列表中,并将第二次调用备用以重新获得时间:

import os
from glob import iglob

# ...

if os.path.isdir(options.map_file_directory):
    modification_times_and_filenames = sorted(
        (os.path.getmtime(n), n)
        for n in iglob(os.path.join(options.map_file_directory, '*.map'))
    )
    for modification_time, filename in modification_times_and_filenames:
        print('{0} - {1}'.format(filename, modification_time))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM