python - 用部分路径名给出变量的完整路径?
问题描述
这是一个更好的版本,包括我到目前为止所做的:
这本词典:sfiles
{'C:\\Users\\user\\Desktop\\05052\\ASTTOM\\ASTTOM': [],
'C:\\Users\\user\\Desktop\\05052\\ASTIK\\ASTIK': [],
'C:\\Users\\user\\Desktop\\05052\\ROADS\\ROADS': []}
我想检查是否sh1
在那里:
sh1
u'ASTTOM'
和
sh2
u'ASTIK'
sfiles
我想将匹配的完整路径分配给 sh1 。
我做了什么 :
for i in list(sfiles):
if shape1 in os.path.basename(i):
print((i))
shape1 = i
这种方法是否正确,我怎样才能有效地为 sh2 做同样的事情?
解决方案
据我了解,您想找到最后一部分有 sh1(称为 shape1)的路径。所以,你可以这样做:
sfiles = {'C:\\Users\\user\\Desktop\\05052\\ASTTOM\\ASTTOM': [],
'C:\\Users\\user\\Desktop\\05052\\ASTIK\\ASTIK': [],
'C:\\Users\\user\\Desktop\\05052\\ROADS\\ROADS': []
}
for pth in sfiles.keys():
if shape1 in pth.split("\\")[-1]:
print(pth)
推荐阅读
- javascript - 为什么创建相同 BroadcastChannel 的多个实例会导致浏览器冻结?
- avro - 读取 3170 字节的 avro 文件会导致 0 条记录,所以我很困惑
- python - py4j.protocol.Py4JNetworkError:Java 端的答案为空
- java - 错误:变量总计可能尚未初始化
- c++ - 为 beaglebone black 交叉编译时无法链接 opencv-3.2.0
- c++ - 如何在c ++中的mysql_fetch_row(MYSQL _RES)中存储值行[i]
- c - 为什么在下面的代码中两个不同的变量具有相同的地址但不同的值?
- javacc - 如何根据 JavaCC 中的用户输入多次调用一个方法?
- javascript - 如何改进正则表达式,使其可以匹配谷歌脚本电子邮件刮板的电子邮件格式?
- webstorm - WebStorm 新版本断点不起作用