Writing to file creates NUL in file

Viewed 20

when I write to a file (for a specfic write) a bunch of NUL's appear in my text document. For example:

main.py

    with open('database.txt', 'r+') as database:
        # Read database for all users
        allusers = database.read()
        allusers = str(allusers)
        # Split the users by splitter '<>'
        splitusers = allusers.split('<>')
        userplace = splitusers[userplace]
        userplace = str(userplace) + str('<>')
        print(userplace)
        allusers_as_list = allusers.split('<>')
        print(allusers_as_list)
        allusers_list_edited = [word for word in allusers_as_list if word not in userplace]
        print(allusers_list_edited)
        writetofile = ' '.join(allusers_list_edited)
        writetofile = str(writetofile) + str('<>')
        print(writetofile)
    with open('database.txt', 'w+') as database:
        database.truncate(0)
        database.write(writetofile)
    with open('usernum.txt', 'r+') as file:
        contents = file.read()
        list_contents = contents.split('\n')
        while ("" in list_contents):
            list_contents.remove("")
        del list_contents[userplace2]
        listToStr = '\n'.join([str(elem) for elem in list_contents])
        file.truncate(0)
        file.write(listToStr)

database.txt (yes I know I could've used an actual database and yes It's a mess but I've come too far now)

5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8,Justin 9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08,test1<>9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08,test2<>9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08,test3<>9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08,test4<>

usernum.txt

Justin
test1
test2
test3
test4

When running (userplace=3) the output in usernum.txt is: Output (NUl doesn't copy into here)

Have I made a basic error or is there something else going on? I'd appreciate any help :). As always if this isn't reproducible and is not helpful on this site I will remove. Thanks

0 Answers
Related