1

Sorry for the title, my problem is as follows. I have a list of paths and I want to get multiple common prefixes. For example, given I have:

['/usr/local/lib/python2.7/dist-packages/pkg_name-0.1-py2.7.egg/pkg_name',
 '/usr/local/lib/python2.7/dist-packages/pkg_name-0.1-py2.7.egg/EGG-INFO',
 '/usr/bin/pkg_name']

I want to have:

['/usr/local/lib/python2.7/dist-packages/pkg_name-0.1-py2.7.egg/',
 '/usr/bin/pkg_name']

Because the first two have a common prefix that is a directory. Hope I made this clear,

rubik

EDIT: The paths I have are Python eggs and some executables. I want to remove the entire egg, not the directories inside it, like EGG-INFO or pkg_name. So it has to be /usr/.../dist-packages/pkg_name-0.1-py2.7.egg/. The other path, since it is an executable remains as it is.

Thank you

rubik
  • 8,814
  • 9
  • 58
  • 88

1 Answers1

2

The problem is not well-defined. What do you want to have in this case:

/usr/bin/a
/usr/bin/b
/usr/etc
/usr/local

Shall it be one /usr or two: /usr/bin /usr, or three?

In either case, the algorithm will be like this:

  1. sort the list
  2. take the first element and do os.path.commonprefix() with 2nd, 3rd, ..., i-th until common prefix is not /; that will be your first group
  3. repeat step 2, starting from (i+1)th
hamstergene
  • 24,039
  • 5
  • 57
  • 72
  • Thank you. I know it is not well-defined, but I don't know how to explain it. I want to have common prefixes that are directory. So in your example it would be: `/usr/bin, /usr/etc/, /usr/local`. But the problem is more defined than that. The paths I have are Python eggs and their executable. I want to remove the entire egg, not the directories inside it (like `EGG-INFO` or `pkg_name`). I should edit my question... – rubik Aug 15 '11 at 07:22
  • I would suggest `os.path.commonpath()` for this operation, not commonprefix. It seems like the behaviour OP wants (and that I want having found this question with a similar use case), is to get the longest possible common path that matches more than one path in the list. So in this case it would be: `['/usr', '/usr/bin']` – Lou Dec 15 '22 at 11:40