File shredding not working properly (the shredded file is not the same size as the original)
02:14 14 Jun 2020

I've been trying to make a file shredder that will overwrite every single byte of any file by a new random one, rename it, and then delete it. I disabled the deletion to check what the output was being like, and for some reason, the output file size was smaller than the original file size (exactly 1 byte of size). Before this happened, it was actually getting bigger that the original file size. I've been messing around with the code for some hours, but have not spot the mistake yet. What is causing it to be smaller?

def shred_file(path : str, passes : int, max_filename : int):
    global dir_char
    valid_chars = string.ascii_letters + string.digits
    valid_bytes = [ b"%c" %byte for byte in range(0x0, 0xFF+1)]
    filename_len = range(1, max_filename + 1)
    if(os.path.isfile(path) == True):
        filesize = os.path.getsize(path)
        for temp in range(0, passes):
            file = open(path, "wb")
            #Overwrite bytes
            for i in range(0, filesize + 1):
                file.seek(i)
                file.write(b"%c" %random.choice(valid_bytes))
            file.close()

            #Rename file
            new_name = str("".join(random.choices(valid_chars, k=random.choice(filename_len))))
            new_path = ""
            if(len(path.split(f"{dir_char}")) > 1):
                new_path = f"{dir_char}".join(path.split(f"{dir_char}")[0:-1]) + f"{dir_char}{new_name}"
            else:
                new_path = f"{dir_char}".join(path.split(f"{dir_char}")[0:-1]) + f"{new_name}"
            os.rename(path, new_path)
            path = new_path
        #os.remove(path)
python file byte file-writing