-2

I have two txt files. This files are created automatically and it also inserts '10' as a text into these files. But if the program reads the files it gives an error like "ValueError: invalid literal for int() with base 10: ''". How can i fix this error?

    income_txta = open(("monthly_income,expences_amount\\income\\" + year + month + ".txt"), "a")
    income_txtr = open(("monthly_income,expences_amount\\income\\" + year + month + ".txt"), "r")
    if income_txtr.read() == '':
        income_txta.write("10")
        total_income=10
    else:
        total_income = int(income_txtr.read())

    expences_txta = open(("monthly_income,expences_amount\\expences\\" + year + month + ".txt"), "a")
    expences_txtr = open(("monthly_income,expences_amount\\expences\\" + year + month + ".txt"), "r")
    if expences_txtr.read() == '':
        expences_txta.write("10")
        total_expences = 10
    else:
        total_expences = int(expences_txtr.read())
        print(total_expences)
L Lochana
  • 79
  • 7
  • Possibly related question [here](https://stackoverflow.com/questions/17142088/python-2-7-and-3-3-2-why-int0-0-does-not-work) – Daemon Painter Apr 24 '20 at 08:36
  • It tells you what's wrong (`ValueError: invalid literal for int() with base 10: ''`), you're probably trying to convert an empty string to an integer with `int("")`. – yvesonline Apr 24 '20 at 08:37
  • That error is thrown because you are trying to convert empty string to int. And that's because you are appending 10 to the file object `income_txta`. What you should really is read the file content into memory not work on the file object. – NomadMonad Apr 24 '20 at 08:38
  • Yes I thought that too. And i even checked these files manually. But actually they are not empty. – L Lochana Apr 24 '20 at 08:39
  • Does this answer your question? [ValueError: invalid literal for int() with base 10: ''](https://stackoverflow.com/questions/1841565/valueerror-invalid-literal-for-int-with-base-10) – Daemon Painter Apr 24 '20 at 08:39
  • No it doesn't answer my question I tried that too – L Lochana Apr 24 '20 at 08:44

1 Answers1

0

I changed the code like this and it worked.

    if os.path.exists("monthly_income,expences_amount\\income\\" + year + month + ".txt"):
        print("total_income_file exists")
    else:
        fh = open("monthly_income,expences_amount\\income\\" + year + month + ".txt", "w")

    if os.path.exists("monthly_income,expences_amount\\expences\\" + year + month + 
         ".txt"):
        print("total_expences_file exists")
    else:
                income_txtr = open(("monthly_income,expences_amount\\income\\" + year + month + ".txt"), "r")
    if income_txtr.read() == '':
        income_txta = open(("monthly_income,expences_amount\\income\\" + year + month + ".txt"), "a")
        income_txta.write("10")
        total_income=10
    else:
        income_txtr = open(("monthly_income,expences_amount\\income\\" + year + month + ".txt"), "r")
        total_income = int(income_txtr.read())


    expences_txtr = open(("monthly_income,expences_amount\\expences\\" + year + month + ".txt"), "r")
    if expences_txtr.read() == '':
        expences_txta = open(("monthly_income,expences_amount\\expences\\" + year + month + ".txt"), "a")
        expences_txta.write("10")
        total_expences = 10
    else:
        expences_txtr = open(("monthly_income,expences_amount\\expences\\" + year + month + ".txt"), "r")
        total_expences = int(expences_txtr.read())
        print(total_expences)fh = open("monthly_income,expences_amount\\expences\\" + year + month + ".txt", 
          "w")
L Lochana
  • 79
  • 7