问题描述
因此,我尝试使用Python生成文本文档,并且希望将生成的文件上载。但是,即使将函数设置为写入模式,也会出现此错误:
代码(text_document_generator.py):
import os
def writetoAndUploadFile(filename,filecontents): # This function generates the text file and returns the file name.
path = "text_documents/"
textfile = open(os.path.join(path,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制作应用程序。我也完成了os.path.join,但是似乎没有用。
解决方法
似乎未创建该文本文件,然后使用:
textfile = open(os.path.join(path,filename + ".txt"),"w+")
如果文本文件不存在,则会自动创建。