Search code examples
pythonpython-2.7listsplitlist-manipulation

Access last string after split function to create new list


I am a beginner in Python and I have been working on a code to access two types of files (dcd and inp files), combine them and create a new list with the matching strings.

I got stuck somewhere at the beginning. I want to get all dcd files here. So they have .dcd extension but the first part is not the same. So I was thinking if there is a way to access them after I have split the string.

    #collect all dcd files into a list
list1 = []
for filename1 in glob.glob('*/FEP_SYAF014*/FEP1/298/*/*.dcd'):
    filename1 = filename1.split('/')
    filename1.sort()    
    list1.append(filename1) 

I want to get only names with dcd extension that are indexed [5] and create a new list or mutate this one, but I am not sure how to do that.

p.s I have just posted first part of the code Thank you ! the oddly sorted part this one looks better

and this is how I would like it to look like, but sorted and without eq* files. want this sorted


Solution

  • So this worked. I just added del filename1[:5] to get rid of other unnecessary string parts import os, glob list1 = sorted(glob.glob('/FEP_SYAF014/FEP1/298//.dcd'), key = os.path.basename)

    for filename1 in sorted(glob.glob('*/FEP_SYAF014 */FEP1/298/*/*.dcd'),key = os.path.basename):
        filename1 = filename1.split('/')
    
        filename1.sort()
        list1.append(filename1)
        del filename1[:5]
        print filename1