【问题标题】:How to get the time of the last modified file in a folder using Python如何使用Python获取文件夹中最后修改文件的时间
【发布时间】:2017-06-28 17:04:30
【问题描述】:

我正在 ODI 中开发代码。我的需要是获取目录中最后修改文件的日期/时间,并检查最后修改文件的日期/时间是否大于 5 分钟;然后将该文件夹中的所有文件复制到另一个文件夹。如果小于 5 分钟,请等待 2 分钟,然后重新检查。

我已经通过 .bat 文件获得了目录中最后修改文件的日期/时间。我将输出存储在 .txt 文件中,然后将该文件加载到临时界面中以检查时间是否大于 5 分钟。

我想通过 Python 脚本来实现我的需求,因为我希望它会在 ODI 程序的一个步骤中完成。

请帮忙。

提前致谢

【问题讨论】:

标签: python oracle-data-integrator


【解决方案1】:

删除文件夹中最后修改的文件(如果文件超过 5 分钟) 没有递归:

import os
import time

folder = 'pathname'

files = [(f, os.path.getmtime(f)) for f in os.listdir(folder) 
                if os.path.isfile(f)]

files = sorted(files, key=lambda x: x[1], reverse=True)

last_modified_file = None if len(files) == 0 else files[0][0]

# get age file in minutes from now
def age(filename):
    return (time.time() - os.path.getmtime(filename))//60

if last_modified_file is not None:
    if age(last_modified_file) >= 5:
        os.remove(last_modified_file)

【讨论】:

  • 嗨.. 谢谢你... 要求的一些变化.. 如果上次修改文件的日期/时间大于 5 分钟,将该文件夹中的所有文件复制到另一个文件夹。 .如果小于5分钟,等待2分钟再重新检查..
猜你喜欢
  • 2011-11-02
  • 1970-01-01
  • 2010-09-27
  • 2014-10-28
  • 1970-01-01
  • 2012-05-13
  • 1970-01-01
  • 2010-10-05
  • 1970-01-01
相关资源
最近更新 更多