我想在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: '/'
os.path.commonprefix(list)
或者,如果您使用的是Python3.4+(我想这部分答案更适合未来),您可以使用
pathlib
和: PurePaths.parts会给你一个将不同文件的元组转换为列表,然后找到common list of prefixes for a list of lists。你知道吗
这是我的密码:
已经在RosettaCode上完成了:http://rosettacode.org/wiki/Find_common_directory_path#Python
相关问题 更多 >
编程相关推荐