定义函数时出现语法错误

时间:2015-10-22 03:16:34

标签: python function python-3.x

在此行的撇号上获取语法错误

def filecopy('example.txt','output.txt'):  #<- Error here on the "'"
    infile = open('example.txt',)
    text = infile.read()
    infile.close()
    infile = open('output.txt')
    outfile.write(text)
    infile.close()

1 个答案:

答案 0 :(得分:2)

你不能在函数声明中使用这样的文字,看起来你在调用函数时会混淆声明:

def filecopy(infile, outfile):
    ...

# Later call the function
filecopy('example.txt','output.txt')

您可以拥有默认参数:

def filecopy(infile='example.txt', outfile='output.txt'):
    ...

# But you still need to call it
filecopy()
# or
filecopy('fred.txt', 'wilma.txt')