-1

I'm making a program for saving passwords (only for me)

with open(file, mode='w', encoding='Latin-1') as FA:
    try:
        FAJ = json.load(FA)
    except Exception:
        tafa = {tarr[0]: {'login': tarr[1], 'password': tarr[2]}}
        json.dump(tafa, FA)
    else:
        FAJ[tarr[0]] = {'login': tarr[1], 'password': tarr[2]}
        json.dump(FAJ, FA)
    finally:
        FAJ.close()
        time.sleep(1.5)
        os.system('pause')
        menu()

I'm trying to catch an error and check if the file is empty. But even if file is not empty it anyways throws exception. Does anyone know what could be a problem?

Jozef Cipa
  • 2,133
  • 3
  • 15
  • 29
crousap
  • 49
  • 1
  • 1
  • 5

1 Answers1

2

You open your file for writing: open(file, mode='w', ...). You cannot read from it, and it also truncates the file. Remove the mode parameter. The default open behavior is to open the file for reading.

DYZ
  • 55,249
  • 10
  • 64
  • 93