Tazgirl
Tazgirl

Reputation: 83

Python/Discord Failure to write in files

In the following code, the files are created and closed properly with no error messages but when it tries to write into the file, nothing happens. No error messages and no 0 written into the file:

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")

All help is appreciated, thank you.

Upvotes: 0

Views: 46

Answers (1)

aerijman
aerijman

Reputation: 2762

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

str

You are trying to write into a string, not into a file handler

do

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

Upvotes: 3

Related Questions