【问题标题】:How to list latest modification time of contents of some directories?如何列出某些目录内容的最新修改时间?
【发布时间】:2015-11-15 09:02:15
【问题描述】:

假设结构如下:

People/
      Jimmy/
           my_Files/
                    work.txt
           report.txt
      Mohammad/
           the_work/
                    something2.sth
                    something2.sth

如您所见,People 目录中的每个职员都有一个目录,每个职员在他/她的目录中都有很多文件或目录。

我想编写一个程序来接收日期作为输入并搜索这些目录并返回哪个职员在该日期之后没有修改任何文件!

我正在使用以下程序列出所有文件:

files_path= list()
for root,dirs,files in os.walk(r"G:\People"):
    for filename in files:
        files_path.append(root+filename)

以及下面的程序来接收它们的最后修改时间:

for single_file in files_path:
    print "last modified: %s" % time.ctime(os.path.getmtime(single_file))
    print "created: %s" % time.ctime(os.path.getctime(single_file))

好吧,两者都工作正常!问题是我无法将结果管理为具有用户对的可读输出:上次修改时间

任何人都可以帮助我如何处理它以获得合理的输出列表吗?

【问题讨论】:

    标签: python algorithm python-2.7 sorting


    【解决方案1】:

    time.strftime 让您可以控制格式:

    time.strftime('%H:%M:%S', time.gmtime(os.path.getmtime(single_file)))
    

    一个小辅助函数:

    def format_time(seconds):
        return time.strftime('%H:%M:%S', time.gmtime(seconds))
    

    成功了 更平易近人:

    print "last modified: %s, created: %s" %(format_time(os.path.getmtime(single_file)),
                                             format_time(os.path.getctime(single_file)))
    

    输出:

    last modified: 13:05:56, created: 13:05:56
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-30
      • 1970-01-01
      • 1970-01-01
      • 2019-01-11
      • 2018-05-29
      • 2011-12-09
      • 1970-01-01
      相关资源
      最近更新 更多