HOME | Blog | YouTube | LinkedIn | About Me         || Calculators    | SoftEng/Tech Posts    | Code/Scripts

Python - List files in a directory in reverse alphabetical order and find the top file using file name pattern

Here is a Python script that does the following:

  • lists files in a directory in reverse alphabetical order following file names
  • using a file pattern, it gets the "newest" file name as the file names have the embedded date


#!/usr/bin/python
import os
import shutil

############################################################
def main():
    source_dir = "./sampleDir"

    isGood = list_files_from_directory(source_dir)
    if isGood:
        print "*** listing successful"
    else:
        print "*** listing FAILED"

    isGood = get_the_newest_file(source_dir, "customer_1000")
    if isGood:
        print "*** get newest file successful"
    else:
        print "*** get newest file FAILED"


############################################################

def list_files_from_directory(source_directory):
    isSuccess = True
    try:
        src_files = os.listdir(source_directory)
        src_files.sort(reverse=True)
        for file_name in src_files:
            full_file_name = os.path.join(source_directory, file_name)
            if os.path.isfile(full_file_name):
                print "full_file_name: " + full_file_name

        isSuccess = True
    except:
        print ":::ERROR: Failed to list from: " + source_directory
        isSuccess = False
    return isSuccess

############################################################

def get_the_newest_file(source_directory, file_pattern):
    isSuccess = True
    try:
        src_files = os.listdir(source_directory)
        src_files.sort(reverse=True)
        for file_name in src_files:
            full_file_name = os.path.join(source_directory, file_name)
            if os.path.isfile(full_file_name) and file_name.find(file_pattern) > -1:
                print "full_file_name: " + full_file_name
                break
        isSuccess = True
    except:
        print ":::ERROR: Failed to list the newest file from: " + source_directory
        isSuccess = False
    return isSuccess



############################################################

if __name__ == "__main__": main()

Here are the files from the sampleDir directory:

-rw-r--r--@  1 almir  staff    0 Dec 19 10:05 1000_2015_12_18_00_10_1124.json
-rw-r--r--@  1 almir  staff    0 Dec 19 10:06 1000_2015_12_18_00_12_1000.json
-rw-r--r--@  1 almir  staff    0 Dec 19 10:06 1000_2015_12_18_00_15_1001.json
-rw-r--r--@  1 almir  staff    0 Dec 19 00:10 2015_12_18_00_10_1124.json
-rw-r--r--@  1 almir  staff    0 Dec 19 00:09 2015_12_19_00_09_1000.json
-rw-r--r--@  1 almir  staff    0 Dec 19 00:09 2015_12_19_00_09_1001.json
-rw-r--r--@  1 almir  staff    0 Dec 19 00:10 2015_12_19_00_10_1124.json
-rw-r--r--@  1 almir  staff    0 Dec 19 10:10 customer_1000_2015_12_18_00_10_1124.json
-rw-r--r--@  1 almir  staff    0 Dec 19 10:10 customer_1000_2015_12_18_00_12_1124.json
-rw-r--r--@  1 almir  staff    0 Dec 19 10:10 customer_1000_2015_12_18_00_14_1124.json
-rw-r--r--@  1 almir  staff    0 Dec 19 10:10 customer_1000_2015_12_18_00_14_1125.json
-rw-r--r--@  1 almir  staff    0 Dec 19 10:11 customer_1000_2015_12_18_00_14_2000.json
-rw-r--r--@  1 almir  staff    0 Dec 19 10:12 customer_1000_2015_12_18_00_14_2001.json
-rw-r--r--@  1 almir  staff    0 Dec 19 10:11 customer_1001_2015_12_18_00_14_2000.json
-rw-r--r--@  1 almir  staff    0 Dec 19 10:11 customer_1002_2015_12_18_00_14_2000.json

-rw-r--r--@  1 almir  staff    0 Dec 19 10:11 customer_1003_2015_12_18_00_14_2000.json


Here is the result after running the above Python program:

full_file_name: ./sampleDir/customer_1003_2015_12_18_00_14_2000.json
full_file_name: ./sampleDir/customer_1002_2015_12_18_00_14_2000.json
full_file_name: ./sampleDir/customer_1001_2015_12_18_00_14_2000.json
full_file_name: ./sampleDir/customer_1000_2015_12_18_00_14_2001.json
full_file_name: ./sampleDir/customer_1000_2015_12_18_00_14_2000.json
full_file_name: ./sampleDir/customer_1000_2015_12_18_00_14_1125.json
full_file_name: ./sampleDir/customer_1000_2015_12_18_00_14_1124.json
full_file_name: ./sampleDir/customer_1000_2015_12_18_00_12_1124.json
full_file_name: ./sampleDir/customer_1000_2015_12_18_00_10_1124.json
full_file_name: ./sampleDir/2015_12_19_00_10_1124.json
full_file_name: ./sampleDir/2015_12_19_00_09_1001.json
full_file_name: ./sampleDir/2015_12_19_00_09_1000.json
full_file_name: ./sampleDir/2015_12_18_00_10_1124.json
full_file_name: ./sampleDir/1000_2015_12_18_00_15_1001.json
full_file_name: ./sampleDir/1000_2015_12_18_00_12_1000.json
full_file_name: ./sampleDir/1000_2015_12_18_00_10_1124.json
*** listing successful
full_file_name: ./sampleDir/customer_1000_2015_12_18_00_14_2001.json
*** get newest file successful


- almirsCorner.com -

#python #programming #code #coding #software #softwaredeveloper #softwareengineering #programmer

No comments:

Post a Comment