我有一个文件路径
filePath="dir1/dir2/dir3/dir4/dir5/file.txt";
如何从路径中提取最后3个级别?输出:
newFilePath="dir4/dir5/file.txt";
您可以使用split和join从某个文件路径中获取最后一个n。例如,
test = "some/dir/somewhere/here/"
new = "/".join(i for i in test.split("/")[-4:])
print(new) # 'dir/somewhere/here/'