【问题标题】:Getting a Syntax error while defining a function定义函数时出现语法错误
【发布时间】:2015-10-22 03:16:34
【问题描述】:

在这一行的撇号上出现语法错误

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

【问题讨论】:

  • 不应该是变量名吗?

标签: python function python-3.x


【解决方案1】:

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

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

【讨论】:

  • @Jerry Scirica 如果对您有帮助,请接受答案。
猜你喜欢
  • 2019-10-22
  • 1970-01-01
  • 2022-08-03
  • 2014-05-24
  • 2018-12-06
  • 2016-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多