import osx = 1while x < 101:if x < 10:file_name = "0"+str(x) + ".txt" #10一下则前面补零file = open(file_name)else:file_name = str(x) + ".txt"file = open(file_name)line = file.readline()file.close()os.rename(file_name,line) #重命名x = x + 1

findall是返回所匹配的字符串,返回的是一个列表,并不返回match对象,match对象才有start,span方法
想找到所有匹配字符串的索引用finditer吧
关键是这个‘^’。另外'(', ')'是正则表达式里边的特殊字符,需要'\(' , '\)'转译一下。