为了账号安全,请及时绑定邮箱和手机立即绑定

Python/Discord 写入文件失败

Python/Discord 写入文件失败

桃花长相依 2023-06-27 17:20:27
在以下代码中,文件被正确创建和关闭,没有错误消息,但当它尝试写入文件时,没有任何反应。没有错误消息,也没有 0 写入文件:if message.content == "MMO start":    dir = r'C:\\Users\\User\Desktop\MMOProfiles'    MessageAuthor = str(message.author)    newpath = os.path.join(dir,MessageAuthor)    doesExist = os.path.exists(newpath)    if doesExist == False:        await message.channel.send("Creating profile")        os.makedirs(newpath,1)        newpath = os.path.join(newpath,"Level.txt")        open(newpath,"x")        newpath.close()        open(newpath,"w")        newpath.write("0")        newpath.close()            if doesExist == True:        await message.channel.send("You already have a profile")感谢所有帮助,谢谢。
查看完整描述

1 回答

?
慕村225694

TA贡献1880条经验 获得超4个赞

newpath = os.path.join(newpath,"Level.txt")
type(newpath)

斯特

您正在尝试写入字符串,而不是文件处理程序

    f = open(newpath,"w")
    f.write("0")
    f.close()


查看完整回答
反对 回复 2023-06-27
  • 1 回答
  • 0 关注
  • 65 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信