Search code examples
pythondjangolistdir

Check if the last file in os.listdir()


I need to know if the file currently checked in the os.listdir is the last file. I need it in a condition I have. Here is the code:

# iterate through all files in directory
for filename in os.listdir(edgar_path):
    # open and read
    with open(edgar_path + filename, 'r') as file:
        # rearrange by date (column 3)
        tsv_file = sorted(list(csv.reader(file, delimiter='|')), key=lambda t: t[3])

        # get date today
        today = datetime.datetime.now()  
        # get start date and end date depending on the first and last row
        start_date = datetime.datetime.strptime(tsv_file[0][3], "%Y-%m-%d")
        end_date = datetime.datetime.strptime(tsv_file[len(tsv_file) - 1][3], "%Y-%m-%d")
        # check print
        logger.debug(start_date)
        logger.debug(end_date)
        # if within date range
        if start_date <= today <= end_date:
            logger.debug('pass condition 1')
        # if not within date range, but
        # the date today is greater than the end date 
        # and its the last file being checked so it still passes
        elif today >= end_date:
            logger.debug('pass condition 2')
        # add to delete files if both conditions are unmet
        else:
            files_to_delete.append(filename)
            logger.debug('no pass')

Any help is appreciated.


Solution

  • I think you can put the filenames in a list variable (filenames in the below code) and check if the filename is the last item in the filenames list by using the condition

    filename == filenames[-1]
    
    # iterate through all files in directory
    filenames = os.listdir(edgar_path)
    for filename in filenames:
        # open and read
        with open(edgar_path + filename, 'r') as file:
            # rearrange by date (column 3)
            tsv_file = sorted(list(csv.reader(file, delimiter='|')), key=lambda t: t[3])
    
            # get date today
            today = datetime.datetime.now()
            # get start date and end date depending on the first and last row
            start_date = datetime.datetime.strptime(tsv_file[0][3], "%Y-%m-%d")
            end_date = datetime.datetime.strptime(tsv_file[len(tsv_file) - 1][3], "%Y-%m-%d")
            # check print
            logger.debug(start_date)
            logger.debug(end_date)
            # if within date range
            if start_date <= today <= end_date:
                logger.debug('pass condition 1')
            # if not within date range, but
            # the date today is greater than the end date
            # and its the last file being checked so it still passes
            elif today >= end_date:
                logger.debug('pass condition 2')
            # add to delete files if both conditions are unmet
            else:
                files_to_delete.append(filename)
                logger.debug('no pass')
        
        # check for last file here
        if filename == filenames[-1]:
            # Do something