1

我正在尝试使用正则表达式来搜索目录中的所有文件,然后最终它将更改文件的名称。

这是我到目前为止所拥有的。

def regExp():
    os.chdir("C:/Users/David/Desktop/Test/Files")
    files = os.listdir(".")
    for x in (files):
        inputFile = open((x), "r")
        content = inputFile.read()
        inputFile.close()
        try:
            regex_txt = ("\(;;\)")
            regex = re.compile(regex_txt)
            print (regex.findall(x))

收到以下错误消息

File "<module1>", line 108
    print (regex.findall(x))
                           ^

SyntaxError:解析时出现意外的 EOF

任何帮助表示赞赏

4

2 回答 2

3

问题是你的块没有一个except或一个finallytry。一个单独的try块是没有意义的,所以 Python 坚持你有

try:
    your code
except SomeException:
    handle it

或者

try:
    your code
finally:
    clean up
于 2012-12-11T18:55:50.060 回答
-3

你应该使用:

re.findAll(regex,text,flags)

如果你想预编译它,我认为你可以这样做:

 my_regex= re.compile(pattern)
 result = my_regex.match(string)
于 2012-12-11T18:30:58.337 回答