如何获取文件上游两个目录的文件夹路径名?
How can I get the pathname of the folder two directories upstream of a file?
使用 glob2 和 os 我想要目录 '/a/b/'
给定文件路径 '/a/b/c/xyz.txt'
我已经能够(递归地)在 glob2 中使用 /*
和 /**
向前移动目录,但不能向后移动父目录。我不想使用正则表达式或拆分。有没有一种简单的方法可以使用 glob and/or os?
为什么是 glob?
dir_path = file_path.split('/')
what_i_want = '/' + dir_path[10] + '/' + dir_path[1] + '/'
您也可以通过查找第三个斜线的索引来完成此操作,使用每个调用的 return 作为下一个调用的 "start" 参数。
third_slash = file_path.index('/', file_path.index('/', file_path.index('/')+1) +1)
what_i_want = file_path[:third_slash+1]
使用 glob2 和 os 我想要目录 '/a/b/'
给定文件路径 '/a/b/c/xyz.txt'
我已经能够(递归地)在 glob2 中使用 /*
和 /**
向前移动目录,但不能向后移动父目录。我不想使用正则表达式或拆分。有没有一种简单的方法可以使用 glob and/or os?
为什么是 glob?
dir_path = file_path.split('/')
what_i_want = '/' + dir_path[10] + '/' + dir_path[1] + '/'
您也可以通过查找第三个斜线的索引来完成此操作,使用每个调用的 return 作为下一个调用的 "start" 参数。
third_slash = file_path.index('/', file_path.index('/', file_path.index('/')+1) +1)
what_i_want = file_path[:third_slash+1]