簡體   English   中英

我想在 Python 中返回文件名。 我怎樣才能正確地做到這一點?

[英]I want to return file name in Python. How can I properly do it?

所以我想在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()

主文件

  @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 制作應用程序有點陌生。

不要調用公共屬性,它不是函數。 嘗試return textfile.name

如果你只需要沒有擴展名的文件名那么,在 writeToAndUploadFile()

   return filename

&如果有擴展然后

   return filename+'.txt'

它應該工作

沒有帶有 .name() 屬性的函數屬性

嘗試這個

Testfile_name = filename + ".txt"
return Testfile_name 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM