簡體   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