问题描述
所以我想用Python生成一个文本文档,但是似乎有一个问题,当我希望它返回文件名时,我会收到此错误:
File "D:\My Applications\ghBot (Python)\text_file_generator.py",line 5,in writetoAndUploadFile
return textfile.name()
TypeError: 'str' object is not callable
text_file_generator.py
def writetoAndUploadFile(filename,filecontents):
textfile = open("text_documents/" + filename + ".txt","w")
textfile.write(filecontents)
textfile.close()
return textfile.name()
main.py
@client.command(aliases = ["newtextdoc"])
async def new_text_doc(ctx,filename,*,filecontents):
await ctx.send("Text document successfully generated.")
await ctx.send(file=discord.File(text_file_generator.writetoandUploadFile(filename,filecontents)))
如果这很明显,对不起。我刚开始使用Python编写应用程序。
解决方法
不要调用common属性,它不是函数。尝试return textfile.name
如果只需要文件名而不带扩展名 然后, 在writeToAndUploadFile()
return filename
&如果带有扩展名,则
return filename+'.txt'
应该可以解决
没有带有.name()属性的函数属性
,尝试一下
Testfile_name = filename + ".txt"
return Testfile_name