2

I want find general directory of all linux files(not directory) in python. Could everyone help me?

Example1:

['/home/tung/abc.txt', '/home/tung/xyz.txt']
    -> general directory: '/home/tung'

Example2:

['/home/tung/abc.txt', '/home/tung/xyz.txt', '/home/user/123.txt']
    -> general directory: '/home'

Example3:

['/home/tung/abc.txt', '/root/xyz.txt']
    -> general directory: '/'
jfs
  • 399,953
  • 195
  • 994
  • 1,670

5 Answers5

5

os.path.commonprefix(list)

Return the longest path prefix (taken character-by-character) that is a prefix of all paths in list. If list is empty, return the empty string (''). Note that this may return invalid paths because it works a character at a time.


Alternatively, if you are using python 3.4+ (I guess this part of the answer is more for the future), you could use pathlib and: PurePaths.parts will give you a

tuple giving access to the path’s various components.

Convert tuples of the different files to lists, and then find the common list of prefixes for a list of lists.

Community
  • 1
  • 1
radomaj
  • 841
  • 8
  • 18
  • 1
    Thanks radomaj! If I use os.path.commonprefix(list), your result will be: ['/home/tung/abc.txt', '/home/tung123/xyz.txt'] -> general directory: '/home/tung' .While the result I want to be: '/home/' :) – user3414482 Mar 13 '14 at 09:58
  • @user3414482: you could [use `commonprefix()` to get `/home` in this case](http://stackoverflow.com/a/22387892/4279) – jfs Jun 02 '14 at 00:30
1

It has been done on RosettaCode: http://rosettacode.org/wiki/Find_common_directory_path#Python

Paddy3118
  • 4,704
  • 27
  • 38
  • This should be the accepted answer, I think. Notes the broken implementation and has much more concise and idiomatic code for the proper solution. – radomaj Jun 01 '14 at 14:50
0

This is my code:

def test(l):
l = [s.split('/') for s in l]
len_list = len(l)
min_len = min(len(s) for s in l)
i = 0
result = ''
while i < min_len:
    j = 1
    while j < len_list:
        if l[0][i] != l[j][i]:
            return result
        j += 1
    result = '%s%s/' % (result, l[0][i])
    i += 1
return result
0

Moving one step further @radomaj's answer, here's commonprefix()-based implementation that produces the correct answer /home instead of (invalid) /home/tung for ['/home/tung/abc.txt', '/home/tung123/xyz.txt'] input files:

#!/usr/bin/env python
import os
try:
    from pathlib import PurePath
except ImportError: # Python < 3.4
    def get_parts(path):
        path = os.path.dirname(path) # start with the parent directory
        parts = []
        previous = None
        while path != previous:
            previous = path
            path, tail = os.path.split(path)
            parts.append(tail)
        parts.append(path)
        parts.reverse()
        return parts
else: # pathlib is available
    def get_parts(path):
        return PurePath(path).parent.parts

def commondir(files):
    files_in_parts = list(map(get_parts, files))
    return os.path.join(*os.path.commonprefix(files_in_parts))

Example:

print(commondir(['/home/tung/abc.txt', '/home/tung123/xyz.txt']))
# -> /home
Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670
0

2020 update using Python3:

def iscommon(a: PurePath, b: PurePath) -> bool:
    """Determine if paths have common root"""
    length = min(len(a.parts), len(b.parts))
    for i in range(length):
        if a.parts[i] != b.parts[i]:
            return False
    return True
gerardw
  • 5,822
  • 46
  • 39