在Python中创建兄弟文件名
问题描述:
在Python 2.7中获取兄弟文件(名称)的idomatic/canoncial /最佳方式是什么?在Python中创建兄弟文件名
也就是说,如果有喜欢的'C:\\path\\file.bin'
或'/path/file.bin'
文件,
如何获得'C:\\path\\anothername.anotherext'
或'/path/anothername.anotherext'
。
字符串操作,搜索最后的路径分隔符,然后替换之后的部分,当然会起作用,但这看起来非常粗糙。
换句话说,什么是地道的Python相当于(S)这些Java代码段:
File sibling = new File(file.getParent(), siblingName);
或为路径字符串长一点:
String siblingPathName = new File(new File(filePathName).getParent(), siblingName).toString();
注:Java的正上方采用的是与问题无关。注2:如果Python 3有一个新的方法,即使我使用Python 2.7 atm,也很高兴知道这一点。