在文件名中打开超过3天日期戳的文件 - Python 2.7

时间:2016-09-21 08:36:30

标签: python python-2.7 datestamp

**问题** 我试图打开(在python中)当前名称中日期戳超过3天的文件。示例:2016_08_18_23_10_00 - JPN - MLB - Mickeymouse v Burgerface.ply。到目前为止,我可以创建一个日期变量,但我不知道如何在文件名中搜索此变量。我认为我需要先将它转换为字符串?

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8"/>
    <title>Kendo UI Grid</title>

    <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.3.914/styles/kendo.common.min.css"/>
    <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.3.914/styles/kendo.silver.min.css"/>

    <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script src="http://kendo.cdn.telerik.com/2016.3.914/js/kendo.all.min.js"></script>
  </head>
  <body>

    <div id="grid"></div>

    <script>
      var sampleData = [
        { id: 1, name: "name", items: ["foo", "bar"] }
      ];

      $(function () {
        var dataSource = new kendo.data.DataSource({
          data: sampleData,
          schema: {
            model: {
              id: "id",
              fields: {
                id: { type: "number" },
                name: { },
                items: { }
              }
            }
          }
        });

        $("#grid").kendoGrid({
          dataSource: dataSource,
          columns: [
            { field: "id" },
            { field: "name" },
            { field: "items", template: "#= showItems(items) #" }
          ]
        });

      });

      function showItems(arr) {
        return "<ul><li>" + arr.join("</li><li>") + "</li></ul>";
      }

    </script>
  </body>
</html>

非常感谢任何帮助

2 个答案:

答案 0 :(得分:0)

您可以使用 // Row one - column one. $(document).on('click', '.test', function() { $(this).animate({ left: '35%', opacity: '0.9', height: '500px', width: '500px' }); $(this).addClass("active"); }); $(document).on('click', '.test.active', function() { $(this).animate({ left: '100px', opacity: '0.5', height: '250px;', left: '0%', width: '14%', height: '100%', opacity: '1' }); $(this).removeClass("active"); }); 。它会将您的字符串(假设格式正确)转换为日期时间对象,您可以根据文件名比较您的文件是否超过3天:

strptime

如果文件名是from datetime import datetime ... lines = [] for filename in os.listdir(path): date_filename = datetime.strptime(filename.split(" ")[0], '%Y_%m_%d_%H_%M_%S') if date_filename < datetime.datetime.now()-datetime.timedelta(days=days_to_subtract): with open(os.path.join(path, filename), 'r') as f: lines.extend(f.readlines()) # put all lines into array ,则会使用2016_08_18_23_10_00 - JPN - MLB - Mickeymouse v Burgerface.ply提取日期时间部分。然后我们可以使用filename.split(" ")[0]

来检查它是否超过三天

答案 1 :(得分:0)

要打开给定目录中包含名称超过3天的时间戳的所有文件:

#!/usr/bin/env python2
import os
import time

DAY = 86400 # POSIX day in seconds
three_days_ago = time.time() - 3 * DAY
for filename in os.listdir(dirpath):
    time_string = filename.partition(" ")[0]
    try:
        timestamp = time.mktime(time.strptime(time_string, '%Y_%m_%d_%H_%M_%S'))
    except Exception: # can't get timestamp
        continue
    if timestamp < three_days_ago: # old enough to open
        with open(os.path.join(dirpath, filename)) as file: # assume it is a file
            for line in file:
                print line,

代码假定时间戳位于本地时区。在C mktime()可以访问the tz database的平台上,可能需要考虑DST转换(如果在您的情况下文件是72或73小时无关紧要,那么只需忽略此段落)。

考虑使用文件元数据,例如“文件的最后修改时间”,而不是从其名称中提取时间戳:timestamp = os.path.getmtime(path)