Python:SyntaxError:关键字arg后的非关键字 [英] Python: SyntaxError: non-keyword after keyword arg

查看:69
本文介绍了Python:SyntaxError:关键字arg后的非关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行以下代码时

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 循环后的第一行.

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

      ^

SyntaxError: non-keyword arg after keyword arg

是什么导致了这个错误?

What is causing this error?

推荐答案

事情就是这样:

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

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

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

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

这篇关于Python:SyntaxError:关键字arg后的非关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆