我有两个txt文件。该文件是自动创建的,并且还会在这些文件中插入“ 10”作为文本。但是,如果程序读取文件,则会出现类似“ ValueError:以10为底的int()无效文字”的错误。我该如何解决此错误?
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)
答案 0 :(得分:0)
我像这样更改了代码,它就起作用了。
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")