英文:
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('yesterdays date:', 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\\'
for file in mfiles:
filetime = [mfile for mfile in glob.glob(path+'*.csv') if test_modified(mfile)]
if filetime.date() == yesterday:
print('The file has yesterdays date')
else:
print('The files doesnt have yesterdays date')
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+'*.csv') 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+'*.csv') if test_modified(mfile)]
is duplicated and I think you should check the logic at this line again.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论