好吧,这可能看起来很简单,但我有很多问题,它是想知道如何做到这一点!你知道吗
在我的文本文件中有一行数字。假设50。这是一个数字,每次我完成一个行动,我想增加1到它。把它存储在一个文本文件中意味着我可以重新启动程序并保留这个号码。你知道吗
我一直这样做:
num_list_location = os.path.join(postnumber_save, "postnumber.txt")
postnum = open(num_list_location, 'a+').read()
comment = 'Post No.: ' + postnum
#Do some thing with code
newpostnum = int(postnum) += 1 #This is what gives the error
open(num_list_location, 'a+').write(str(newpostnum))
然后我想用newpostnum把它写下来继续这个程序!你知道吗
所以它不工作,只是通过返回错误,所以我想知道什么是最好的方式来做我所要求的!谢谢
编辑
为了解决我的问题,感谢史蒂文,我的问题是“a+”,如果你把它改为“r”的读一个和“w”的写一个,然后它的工作
postnum = open(num_list_location, 'r').read()
open(num_list_location, 'w').write(str(newpostnum))
另一个需要改变的是:
newpostnum = int(postnum) += 1
至
newpostnum = int(postnum) + 1
现在一切正常!你知道吗
为了解决我的问题,感谢史蒂文,我的问题是“a+”,如果你把它改为“r”的读一个和“w”的写一个,然后它的工作
另一个需要改变的是:
至
现在一切正常!你知道吗
相关问题 更多 >
编程相关推荐