2015-10-18 80 views
0

我不知道什么是错在此代码:“对于我在文件名中”创建一个类型错误

import os 
import re 
regex = re.compile(r'\d+') 

for f in os.listdir("F:/temptest2/"): 
    fname = os.path.splitext(f)[0] 
    fext = os.path.splitext(f)[1] 
    if fext == ".DNG": 
     fdngnum = regex.findall(fname) 
     matchcount = 0 
     for i in os.listdir("F:/temptest2/"): 
      f2name = os.path.splitext(i)[0] 
      if fdngnum in f2name: 
       matchcount = matchcount + 1 
     if matchcount < 2: 
      print(f) 

我上线if fdngnum in f2name:

TypeError: 'in <string>' requires string as left operand, not list

以下错误它说f2name应该是一个字符串而不是一个列表。但是这不是一个字符串? (我认为它充当单个字符串,每个通过for循环)

谢谢!

+0

“它说f2name应该是一个字符串,而不是一个列表。”不,它说fdng​​num应该是一个字符串。左边是另一边。 – Evert

回答

0

re.findall()返回一个列表。也许你打算重复它,或者只是使用search()

1

这是因为findall函数返回一个列表:

re.findall(模式,字符串,旗帜= 0)

Return all non-overlapping matches of pattern in string, as a list of strings. The string is scanned left-to-right, and matches are returned in the order found. If one or more groups are present in the pattern, return a list of groups; this will be a list of tuples if the pattern has more than one group. Empty matches are included in the result unless they touch the beginning of another match.

检查Documentation其他方法可以帮助你;) (您也可以遍历该列表)。

相关问题