【问题标题】:Python: SyntaxError: non-keyword after keyword argPython:语法错误:关键字 arg 之后的非关键字
【发布时间】:2020-08-10 11:12:42
【问题描述】:

当我运行以下代码时

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

是什么导致了这个错误?

【问题讨论】:

  • 我认为编码是为了追随'r'

标签: python


【解决方案1】:

这就是它所说的:

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

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

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

【讨论】:

    【解决方案2】:

    要真正弄清楚这一点,这是我的初学者答案: 您以错误的顺序输入了参数。
    关键字参数具有这种风格:

    nullable=True, unique=False
    

    应定义一个固定参数:True、False 等。 非关键字参数不同:

    name="Ricardo", fruit="chontaduro" 
    

    此语法错误要求您首先将name="Ricardo" 及其所有类型(非关键字)放在 之前 类似nullable=True.

    【讨论】:

    猜你喜欢
    • 2017-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多