twegener at fastmail.fm
Wed May 29 09:36:02 CST 2013
On Wed, May 29, 2013, at 01:03 AM, Spyro Polymiadis wrote:
I have a python file querying a database to return a list of paths with
which is stored in a variable called 'paths'
How is it possible for me to go through the list – and remove all the
entries with "subfolders"
And only leave me with the "top level" 'folder' path?
def remove_subfolders(folders, sep='/'):
"""Remove folders from that list that are descendants of other
folders -- List of path strings. E.g. ['/some/folder',
sep -- Path separator. Default: '/'
Return a new list with the descendant folders removed.
folders_set = set(folders)
filtered_folders = 
for folder in folders:
parts = folder.split(sep)
is_descendant = False
for num_parts in range(1, len(parts)):
sub_path = sep.join(parts[:num_parts])
if sub_path in folders_set:
is_descendant = True
if not is_descendant:
input_folders = [
filtered_folders = remove_subfolders(input_folders)
assert filtered_folders == ['/path/to/folder',
What would be nicer is if there is an option I can add to my select
statement in postgres to only return the right folders.
That sounds tough, since the check for each row would depend on every
other row, and what it needs to check is variable length (in terms of
Any ideas welcome :)
This message was scanned by ESVA and is believed to be clean.
LinuxSA WWW: http://www.linuxsa.org.au/ IRC: #linuxsa on
To unsubscribe or change your options:
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the linuxsa