Python: SyntaxError: non-keyword after keyword arg
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.
^
SyntaxError: non-keyword arg after keyword arg
What is causing this error?
Solution 1:
It's just what it says:
inputFile = open((x), encoding = "utf8", "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")
Solution 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.
python keywordnon-keywordsyntax-error