如何查找文件夹中最近创建的两个文件的日期?

huangapple go评论76阅读模式
英文:

How to find the latest two created files date in a folder?

问题

我有一个文件夹其中每天会创建两个新的不同名称的CSV文件我需要获取每天创建的最新两个文件的日期以验证这些日期是否对应前一天的日期

我目前正在使用一个代码我从其他帖子中获取到的用于搜索的代码但是我遇到了一个错误错误信息如下

> AttributeError: 'list' object has no attribute 'date'

```python
import os
import glob
import time
from datetime import datetime
import datetime as dt
from pathlib import Path
from datetime import timedelta

yesterday = (datetime.today() - timedelta(days=1)).date()
print('昨天的日期:', yesterday)

def test_modified(filename):
    delta = time.time() - os.path.getmtime(filename)
    delta = delta / (60*60*24)
    if delta < 1:
        return True
    return False

path='C:\\testfolder\\'

mfiles = glob.glob(path+'*.csv')
filetime = [mfile for mfile in mfiles if test_modified(mfile)]  
for file in filetime:
    file_date = (datetime.fromtimestamp(os.path.getctime(file))).date()
    if file_date == yesterday:
        print('文件具有昨天的日期')
    else:
        print('文件不具有昨天的日期')
英文:

I have a folder where a set of two new different CSV files with different names are created daily, and I need to get the dates of the latest two created each day to verify if the dates correspond to the day previous.

Im currently using a code that I got to set up searching through several other posts but I'm getting a error that states:

> AttributeError: 'list' object has no attribute 'date'

import os
import glob
import time
from datetime import datetime
import datetime as dt
from pathlib import Path
from datetime import timedelta

yesterday = (datetime.today() - timedelta(days=1)).date()
print(&#39;yesterdays date:&#39;, yesterday)

def test_modified(filename):
    delta = time.time() - os.path.getmtime(filename)
    delta = delta / (60*60*24)
    if delta &lt; 1:
        return True
    return False

path=&#39;C:\\testfolder\\&#39;

for file in mfiles:
    filetime = [mfile for mfile in glob.glob(path+&#39;*.csv&#39;) if test_modified(mfile)]  
    if filetime.date() == yesterday:
        print(&#39;The file has yesterdays date&#39;)
    else:
        print(&#39;The files doesnt have yesterdays date&#39;)

My guess is that I must include a Date attribute with the filetime list, but I don't know how.

Any help would be greatly appreciated!

答案1

得分: 1

I think these 2 lines cause the problem:

filetime = [mfile for mfile in glob.glob(path+'*.csv') if test_modified(mfile)]
if filetime.date() == yesterday:

执行列表理解后,您的 filetime 变量只是一个文件csv路径的字符串列表,没有任何日期属性。

这个帖子也许可以帮助您解决这个问题:
https://stackoverflow.com/questions/237079/how-do-i-get-file-creation-and-modification-date-times

另外,

这一行 [mfile for mfile in glob.glob(path+'*.csv') if test_modified(mfile)] 是重复的,我认为您应该再次检查这一行的逻辑。

英文:

I think these 2 lines cause the problem:

filetime = [mfile for mfile in glob.glob(path+&#39;*.csv&#39;) if test_modified(mfile)]  
if filetime.date() == yesterday:

Your filetime variable after execute the list comprehension is just a list string of file csv path and it doesn't have any date attribute.

This thread maybe help you resolve this problem
https://stackoverflow.com/questions/237079/how-do-i-get-file-creation-and-modification-date-times

Also

This line [mfile for mfile in glob.glob(path+&#39;*.csv&#39;) if test_modified(mfile)] is duplicated and I think you should check the logic at this line again.

huangapple
  • 本文由 发表于 2023年3月8日 18:29:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/75671863.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定