Home > Mobile >  Python writing data only on first line of another file
Python writing data only on first line of another file

Time:04-01

I am trying to remove line with specific word from file and write it to another file. The idea is : i have building_objects and after construction is finished I must to delete line with building from file, and write it to file "finished_objects" with date, my code deleting line from building_objects and write in finished_objects, but only to the first line. It deleting existing data in finished_objects and put data from building_objects to the first line

I need to make newlines to write then data from building_objects, how can I do this?

def finish_object():
    tomorrow = datetime.datetime.now()
    finish_object = input("Write name of building:")
    with open("build_objects.txt", "r",encoding="UTF8") as w:
        lines = w.readlines()
    with open("build_objects.txt", "w",encoding="UTF8") as w,open('finished_objects.txt', 'w',encoding="UTF8") as destination:
        for line in lines:
            if finish_object not in line:
                w.write(line)
            else:
                destination.write(line)
                tomorrow = tomorrow   datetime.timedelta(days=1)
                destination.write(',')
                destination.write('{:%d/%m/%Y}'.format(tomorrow))
                destination.write('\n')

building_objects.txt:

Place,building,quantity
New York,cinema,3

CodePudding user response:

First load full finished_objects as list of lines, next run loop to replace first line on list (or insert as first line before other lines), and after loop write all lines again.

Something like this (but not tested):

def finish_object(building_name):
    tomorrow = datetime.datetime.now()   datetime.timedelta(days=1)
    tomorrow_str = tomorrow.strftime('%d/%m/%Y')
    
    # --- read all files ---
    
    with open("build_objects.txt", "r", encoding="UTF8") as w:
        build_objects_lines = w.readlines()
        
    with open('finished_objects.txt', 'r', encoding="UTF8") as w:
        finished_objects_lines = w.readlines()

    # --- change lines ---

    found = False
    
    for number, line in enumerate(build_objects_lines):
        if building_name in line:
            new_line = line.rstrip('\n')   ',{}\n'.format(tomorrow_str)
            finished_objects_lines.insert(0, new_line)  # insert before other lines
            del build_objects_lines[number]
            found = True
            break  # I assume this building is only once in file
            
    # --- write all files ---
    
    if not found:
        print("I can't find:", building_name)
    else:        
        with open("build_objects.txt", "w", encoding="UTF8") as w:
            for line in build_objects_lines:
                w.write(line)
                    
        with open("finished_objects.txt", "w", encoding="UTF8") as w:
            for line in finished_objects_lines:
                w.write(line)
        
# --- main ---

name = input("Write name of building:")
finish_object(name)
  • Related