字符串中的数据在python中变得不可用
我在一个文件中输入了一个数字,但下一次我用Open()命令打开它时,我把这个数字(字符串)放在一个变量int()中,但是这个数字不见了字符串中的数据在python中变得不可用,python,Python,我在一个文件中输入了一个数字,但下一次我用Open()命令打开它时,我把这个数字(字符串)放在一个变量int()中,但是这个数字不见了 file = open('times.txt', 'r') # wrong = 4 print(file.read()) # wrong = 4 wrong = list(file.read()) # there is a problem, when is str not problem but if int => wrong = 0 print(w
file = open('times.txt', 'r') # wrong = 4
print(file.read()) # wrong = 4
wrong = list(file.read()) # there is a problem, when is str not problem but if int => wrong = 0
print(wrong) # wrong = 0
首次调用file
上的read()
后,光标将设置到文件的末尾
如果要再次读取文件的内容,可以使用:
这将返回到文件的开头。值得一提的是,如果他们计划多次读取内容,将输出保存在变量中可能是最好的(除非他们预期文件很大)。
file.seek(0)