我想在 python 中找到所有 linux 文件的通用目录(不是目录)。大家能帮帮我吗?
示例 1:
['/home/tung/abc.txt', '/home/tung/xyz.txt']
-> general directory: '/home/tung'
例子2:
['/home/tung/abc.txt', '/home/tung/xyz.txt', '/home/user/123.txt']
-> general directory: '/home'
示例 3:
['/home/tung/abc.txt', '/root/xyz.txt']
-> general directory: '/'
最佳答案
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.
或者,如果您使用的是 python 3.4+(我想这部分答案更适合 future ),您可以使用 pathlib
和:
PurePaths.parts会给你一个
tuple giving access to the path’s various components.
将不同文件的元组转换为列表,然后找到common list of prefixes for a list of lists .
关于python - 在python中查找所有linux文件的总目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22373771/