Andrew D
Andrew D

Reputation: 37

Extract file name from a sub string

I have to find the file name in the following scenario

I hv structure like .. folder_folder_filename .. sometime i have _ in file name in the last.like ..ABC_.pdf how can I extract the file name in that case

example of file name Abc_12_test_.pdf ---> output ---test.pdf

bc_122_testfile_.pdf->output-->testfile.pdf

test_.xlsx---Output --> test.xlsx

it is not always the case that folder be part of the string.

so if _.exists in last of file name.. I have to find file name between two _.

here is sameple code .. it is not working for all the cases.

        file_name= file_name.split('_')[-1]
        if file_name[0]=='.':
            if "_" not in file_name:
                pattern = "(.*?)_."
                print('pattern 1')
            else:
                pattern = "_(.*?)_."
                print('pattern 2')
            file_name = os.path.join(root, name)
            file_name = re.search(pattern, file_name).group(1)enter code here

Upvotes: 0

Views: 70

Answers (1)

Chris
Chris

Reputation: 29742

IIUC, make it look for non _ characters:

def filename(s):
    name, ext = os.path.splitext(os.path.basename(s))
    name = re.findall("[^_]+", name)[-1]
    return name + ext

tests = [
    "Abc_12_test_.pdf",
    "bc_122_testfile_.pdf",
    "test_.xlsx"
]
for t in tests:
    print(f"{t} --> {filename(t)}")

Output:

Abc_12_test_.pdf --> test.pdf
bc_122_testfile_.pdf --> testfile.pdf
test_.xlsx --> test.xlsx

Upvotes: 1

Related Questions