Python:SyntaxError:关键字arg之后的非关键字

41 python

当我运行以下代码时

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")
Run Code Online (Sandbox Code Playgroud)

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

      ^

SyntaxError: non-keyword arg after keyword arg
Run Code Online (Sandbox Code Playgroud)

导致此错误的原因是什么?

Bre*_*arn 75

这就是它所说的:

inputFile = open((x), encoding = "utf8", "r")
Run Code Online (Sandbox Code Playgroud)

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

inputFile = open((x), "r", encoding = "utf8")
Run Code Online (Sandbox Code Playgroud)

  • 几秒钟打败我.这次的荣耀是你的:) :) (9认同)