user6594048 user6594048 - 23 days ago 9
Python Question

How to compare the name of one file with a number?

I want to select the traces with the number that are more than 60.

Example of file name:

bloc119_5950_6000.npy

The number of this file is:
119

But the error is it displays all th number of all files without any comparison:

def Extract_Number_Block(block):
NbrBlock = block.split('bloc')[1].split('_')[0]
return NbrBlock

path_For_Numpy_blocks='/data/sas/blockData'

for block in os.listdir(path_For_Numpy_blocks):
NbrB= Extract_Number_Block(block)
print(NbrB)
A=60
if NbrB>A:
print(NbrB)
else:
print('Erreur')

Answer Source

Your function returns a string. A comparison occurs with int

def Extract_Number_Block(block):
    NbrBlock = block.split('bloc')[1].split('_')[0]
    return int(NbrBlock) #converting to int

path_For_Numpy_blocks='/data/sas/blockData'

for block in os.listdir(path_For_Numpy_blocks):
    NbrB= Extract_Number_Block(block)
    print(NbrB)
    A=60
    if NbrB>A:
        print(NbrB) 
    else:
        print('Erreur')