>我有一个完整文件路径的列表:
filelist = [
"C:Folder1Filesfileabc.txt",
"C:Folder1Filesfiledef.txt",
"C:Folder2Datafile123.txt"
]
我想按其基本名称(扩展名(在列表中查找文件,但没有指定完整路径。
我试过这样的事情:
name = "filedef.txt"
if name in filelist:
print "Found"
但它不起作用。
有什么提示吗?
你需要做两件事。首先,遍历数组。第二,逃避特殊字符。
paths = [r'C:Folder1Filesfileabc.txt', r'C:Folder1Filesfiledef.txt', r'C:Folder2Datafile123.txt']
name = 'filedef.txt'
for path in paths:
if name in path:
print('Found', path)