def return_filename_test_case(filepath): filename = os.path.basename(filepath) testcase = filename.partition('_')[0] return filename, testcase
On 21 July 2018 at 12:37, Ganesh Pal <ganesh1...@gmail.com> wrote: > I have one of the dictionary values in the below format > > '/usr/local/ABCD/EDF/ASASAS/GTH/HELLO/MELLO/test04_Failures.log' > '/usr/local/ABCD/EDF/GTH/HEL/OOLO/MELLO/test02_Failures.log' > '/usr/local/ABCD/EDF/GTH/BEL/LO/MELLO/test03_Failures.log' > > I need to extract the file name in the path example, say test04_Failure.log > and testcase no i.e test04 > > > Here is my solutions: > > gpal-cwerzvd-1# vi filename.py > import re > > Common_dict = {} > Common_dict['filename'] = > '/usr/local/ABCD/EDF/GTH/HELLO/MELLO/test04_Failures.log' > > def return_filename_test_case(filepath): > if filepath: > filename = re.findall(r'(test\d{1,4}_\S+)', filepath) > if filename: > testcase = re.findall(r'(test\d{1,4})', ''.join(filename)) > > return filename, testcase > > > if Common_dict['filename']: > path = Common_dict['filename'] > fname, testcase = return_filename_test_case(path) > print fname, testcase > > > op: > qerzvd-1# python filename.py > ['test04_Failures.log'] > ['test04'] > > > Please suggest how can this code can be optimized further looks messy , > what would be your one liner or a simple solution to return both test-case > no and filename > > I am on Python 2.7 and Linux > > > Regards, > Ganesh > -- > https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list