How to get all filenames (ex: 01234.xlsb) greater than 00918.xlsb from a folder?

Viewed 33

I have a folder with thousands of files, from 00001.xlsb, 00002.xlsb to 12428.xlsb (increasing everyday). In Python, to get the list of all of them I use (fichier means file, in French):

fichiers = np.array([fichier for fichier in glob.glob("./brut/" + "/*.xlsb")])

I get all files but I don't want files from 00001.xlsb to 00918.xlsb How can I write such a condition in the above?

2 Answers

Maybe something like that:

import os,glob

def castAsInt(fileName):
    #manage the exception if file name cannot  be casted as int => return 0
    try:
        return int (fileName.split(".")[0])
    except :
        return 0


expectedDir = "/home/renaud/Bureau/Python/Test/*" 
MinValue= 918

#All Files
fileList=[os.path.basename(fichier) for fichier in glob.glob(expectedDir,recursive=True)]
print("All Files")
print(fileList)

#Absolute links
fileList=[os.path.basename(fichier) for fichier in glob.glob(expectedDir,recursive=True) if castAsInt(os.path.basename(fichier))>MinValue]
print("Absolutes Links")
print(fileList)

#Relaive links
print("Relative Links")
fileList=[fichier for fichier in glob.glob(expectedDir,recursive=True) if castAsInt(os.path.basename(fichier))>MinValue]
print(fileList)

Result:

All Files
['0002XXX.ods', '1000.ods', '1001.ods', '0002.ods', '0001.ods']
Absolutes Links
['1000.ods', '1001.ods']
Relative Links
['/home/renaud/Bureau/Python/Test/1000.ods', '/home/renaud/Bureau/Python/Test/1001.ods']

Add another line below that:

fichiers = [fichier for fichier in fichiers if int(fichier.split('.')[0]) > 918]
Related