Find general directory of all linux files in python -


i want find general directory of linux files(not directory) in python. 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: '/' 

os.path.commonprefix(list)

return longest path prefix (taken character-by-character) prefix of paths in list. if list empty, return empty string (''). note may return invalid paths because works character @ time.


alternatively, if using python 3.4+ (i guess part of answer more future), use pathlib and: purepaths.parts give

tuple giving access path’s various components.

convert tuples of different files lists, , find common list of prefixes list of lists.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -