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)
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")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.