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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14247732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 10:52:35  来源:igfitidea点击:

Python: SyntaxError: non-keyword after keyword arg

python

提问by

When I run the following code

当我运行以下代码时

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")

I get the following error message which points to the first line after the for loop.

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

      ^

SyntaxError: non-keyword arg after keyword arg

What is causing this error?

是什么导致了这个错误?

回答by BrenBarn

It's just what it says:

这就是它所说的:

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

You have specified encodingas a keyword argument, but "r"as a positional argument. You can't have positional arguments after keyword arguments. Perhaps you wanted to do:

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

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