43

当我运行以下代码时

def regEx1():
  os.chdir("C:/Users/Luke/Desktop/myFiles")
  files = os.listdir(".")
  os.mkdir("C:/Users/Luke/Desktop/FilesWithRegEx")
  regex_txt = input("Please enter the website your are looking for:")
  for x in (files):
    inputFile = open((x), encoding = "utf8", "r")
    content = inputFile.read()
    inputFile.close()
    regex = re.compile(regex_txt, re.IGNORECASE)
    if re.search(regex, content)is not None:
      shutil.copy(x, "C:/Users/Luke/Desktop/FilesWithRegEx")

我收到以下错误消息,它指向 for 循环后的第一行。

      ^

SyntaxError: non-keyword arg after keyword arg

是什么导致了这个错误?

4

2 回答 2

79

这就是它所说的:

inputFile = open((x), encoding = "utf8", "r")

您已指定encoding为关键字参数,但指定为"r"位置参数。关键字参数之后不能有位置参数。也许你想做:

inputFile = open((x), "r", encoding = "utf8")
于 2013-01-09T22:36:57.070 回答
2

To really get this clear, here's my for-beginners answer: You inputed the arguments in the wrong order.
A keyword argument has this style:

nullable=True, unique=False

A fixed parameter should be defined: True, False, etc. A non-keyword argument is different:

name="Ricardo", fruit="chontaduro" 

This syntax error asks you to first put name="Ricardo" and all of its kind (non-keyword) before those like nullable=True.

于 2020-08-10T02:42:14.930 回答