Hello!
I'm new to Python,so I'm sorry if it's a simple question.
I have a text file where each line contains a path.Under some of the "final" subdirectories there are 1 or 2 lines that contain some values.The text file looks like this:

folder1
folder1\folder2
folder1\folder2\folder3
'something1','something2'
folder4
folder4\folder5
folder4\folder5\folder6
'something3','something4'
'something5','something6'
folder4\folder5\folder7
folder4\folder5\folder7\folder8
folder5
etc

I would really appreciate some help on writing a script that will keep only the "final" subdirectories and the values in the file(or write them to a new text file).Something like this:

folder1\folder2\folder3
'something1','something2'
folder4\folder5\folder6
'something3','something4'
'something5','something6'
folder4\folder5\folder7\folder8
etc
Thank you in advance!

I would use something like this. This code assumes that lines starting with ' contain values and the other lines are folder lines.

def filter(lines):
    last = None
    for line in lines:
        line = line.rstrip()
        if line.startswith("'"):
            if last is not None:
                yield last
                last = None
            yield line
        else:
            if not (last is None or line.startswith(last)):
                yield last
            last = line
    if last is not None:
        yield last

with open('infile.txt') as infile, open('outfile.txt','w') as outfile:
    for line in filter(infile):
        outfile.write(line + '\n')

Edited 2 Years Ago by Gribouillis

Thank you and I'm sorry for bothering you again,but unfortunately,it still doesn't work...
With this script,only the values appear in the new text file,while I want the final subdirectories to appear as well.For example if I have these paths in my original text file:

folder1      #path1
folder1\folder2    #path2
folder1\folder2\folder3   #path3
folder1\folder2\folder4   #path4

the new text file should have only path3 and path4:

folder1\folder2\folder3   #path3
folder1\folder2\folder4   #path4

since path1 and path2 are already "in" path3 and path4.

This article has been dead for over six months. Start a new discussion instead.