[英]How can I get the pathname of the folder two directories upstream of a file?
Using glob2 and os I would like the directory '/a/b/'
given the file path '/a/b/c/xyz.txt'
使用glob2和os,我想要目录'/a/b/'
,文件路径为'/a/b/c/xyz.txt'
I have been able to (recursively) move forward through directories using /*
and /**
in glob2, but not backwards through parent directories. 我已经能够(递归)在glob2中使用/*
和/**
在目录中前进,但不能在父目录中前进。 I don't want to use regular expressions or split. 我不想使用正则表达式或拆分。 Is there a simple way to do this using glob and/or os? 是否有使用glob和/或os的简单方法?
Why glob? 为什么是球?
dir_path = file_path.split('/')
what_i_want = '/' + dir_path[10] + '/' + dir_path[1] + '/'
You can also do this by finding the index of the 3rd slash, using the return of each call as the "start" argument to the next. 您也可以通过找到第三个斜杠的索引来实现此目的,并将每个调用的返回用作下一个的“开始”参数。
third_slash = file_path.index('/', file_path.index('/', file_path.index('/')+1) +1)
what_i_want = file_path[:third_slash+1]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.