So I'm trying to generate a text document in Python and I want the generated file to be uploaded. However, even if I set the function to write mode, I get this error:
Code (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
Code (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)))
If this is obvious, sorry. I'm slightly new to making apps in Python. I've done os.path.join as well but it doesn't seem to work.
The text file dosen't seem to be created, then use:
textfile = open(os.path.join(path, filename + ".txt"), "w+")
The text file will be created automatically if does not exist.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.