0

I clear the file, write data to it. The program ends. Then I open the file again, but I choose not to clear the file and write more data there. Why, when I read files with the output_file function, only the information I wrote to it the first time is read? While opening the text file itself, I see that the data has been written there. Help solve the problem please

import pickle
from datetime import *

def AddToFile(fileName):
    file = open(fileName, 'ab')
    products = []
    flag = True
    while flag:
        product = {
            "name":str(input("Name: ")),
            "firstDate":str(input("Date of issue: ")),
            "lastDate":str(input("Expiry date: "))
        }
        print("Added")
        products.append(product)
        ans = str(input("Continue?(Y/N) "))
        if ans == "N" or ans == "n":
            flag = False
    pickle.dump(products,file)
    file.close()
    return products

def clearFile(file):
    clear_file = open(file,'wb')
    clear_file.close()
def WantToClear(file1, file2, file3):
    ans = input("Do you want to clear file? (Y/N) ")
    while ans != 'N' and ans != 'n' and ans != 'Y' and ans != 'y':
        ans = str(input("Incorrect answer.\nPress Y if you want to clear file, in another case press N: "))
    if ans == "Y" or ans == "y":
        clearFile(file1)
        clearFile(file2)
        clearFile(file3)

def output_file(fileName):
    file = open(fileName, 'rb')
    products = pickle.load(file)
    for product in products:
        print(product["name"]+ ' ' + product["firstDate"] + ' ' + product["lastDate"] + ' ' )
    file.close()


def isOverdue(product):
    today = date.today()
    dayToday = today.strftime("%d.%m.%Y")
    lastdate = product["lastDate"]
    t1 = datetime.strptime(dayToday, "%d.%m.%Y")
    t2 = datetime.strptime(lastdate, "%d.%m.%Y")
    day = t2-t1
    Days = day.days
    if Days <= 0:
        return True
    else:
        return False

def datesDiff(product):
    firstdate = product["firstDate"]
    lastdate = product["lastDate"]
    t1 = datetime.strptime(firstdate, "%d.%m.%Y")
    t2 = datetime.strptime(lastdate, "%d.%m.%Y")
    day = t2-t1
    return day.days
def allDiffs(products):
    diffs = []
    for product in products:
        day = datesDiff(product)
        diffs.append(day)
    return diffs
def TwoFiles(fileP, fileL, products, diffs):
    file1 = open(fileP,'ab')
    file2 = open(fileL,'ab')
    perishProds = []
    longTProds = []
    for i in range(0, len(diffs)):
        if isOverdue(products[i]):
            continue
        else:
            if diffs[i] <=5:
                perishProds.append(products[i])
            else:
                longTProds.append(products[i])

    pickle.dump(perishProds,file1)
    pickle.dump(longTProds,file2)
    file2.close()
    file1.close()

fileMain = "NewFilePy.dat"
fileP = "Perishable.dat"
fileL = "Long_term.dat"
WantToClear(fileMain, fileP, fileL)
products = AddToFile(fileMain)
dateDiffs = allDiffs(products)
TwoFiles(fileP,fileL,products,dateDiffs)
output_file(fileMain)
print("Perishable:")
output_file(fileP)
print("Long-term:")
output_file(fileL)
DoB
  • 1
  • 2
  • @XxJames07- Opening a file with write mode clears it. – mkrieger1 Apr 21 '22 at 15:43
  • @mkrieger1 - yes, thanks. But can I output not like a list, without parentheses? – DoB Apr 21 '22 at 16:32
  • Yes, however that seems like an entirely different question. – mkrieger1 Apr 21 '22 at 16:35
  • You can't append to a pickle file. The pickle module writes a single Python object. If you want a variable number of objects in the pickle file, then: the first time make a list containing one object you want to save; the second time, unpickle the 1-list you just wrote, append the new object to the 1-list so it is now a 2-list , and pickle the 2-list. Rinse & repeat. – BoarGules Apr 21 '22 at 16:59

0 Answers0