-1

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
    Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Blue Robin Mar 09 '23 at 04:40
  • 1
    You defined `filetime` as a list. Why are you surprised that a list has no method `date()`? – Pranav Hosangadi Mar 09 '23 at 09:47
  • Check you code again. Is this a copy/paste from somewhere? As already pointed out, you assign a list to `filetime`,of course it does not have a `.date()` method. Before the `for` loop you do the same list comprehension. – foobarna Mar 09 '23 at 09:49

1 Answers1

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:

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 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.

Dat Le
  • 11
  • 2