首页 > 解决方案 > 我不断收到 ValueError: invalid literal for int() with base 10: 错误。由于某种原因,无法将字符串转换为 int

问题描述

FILE_NAME = 'last_seen.txt'
def read_last_seen(FILE_NAME):
    file_read = open(FILE_NAME, 'r')
    last_seen_id = int(file_read.read().strip())
    file_read.close()
    return last_seen_id
id = read_last_seen(FILE_NAME)
print(id)

它说第 20 行有一个错误。我正在尝试为 twitter 提及编写代码,该代码由数字 id 标识。但是,我不断收到此错误。

标签: python

解决方案


好的。这很尴尬。

经过几个小时的尝试,我删除了文本文件,创建了一个新文件。

不知何故,代码无法访问文本文件。所以,删除它并创建一个新的文本文件为我做了!


推荐阅读