Delete 15 characters from last of everyline in a txt file

def delchar(data):
    """ takes input and deletes 16 chars from end (including "\n")"""
    data = list(data)
    data[-16:] = ""
    data = "".join(data)
    return data


mylist = []
newlist = []
with open("del.txt"as fp:
    mylist = fp.readlines()
    # change
    for i in range(0len(mylist)):
        newdata = delchar(mylist[i])
        newlist.append(newdata)     # store new data 

    # overwrite
    with open("del.txt"'w'as f1:
        # first clean the file then append the new data
        f1.write("")
        for i in range(0len(newlist)):
            f1.write(newlist[i] + "\n")
        

    

Comments

Popular Posts