简体   繁体   中英

No such file or directory even though it is set to write mode. How to fix this problem and how can I properly do it?

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM