【发布时间】:2020-11-09 00:10:25
【问题描述】:
我正在使用 python 读取 firstline :
with open(file_path, 'r') as f:
my_count = f.readline()
print(my_count)
我对变量 my_count 的范围有点困惑。虽然打印工作正常,但最好先在外面做 my_count = 0 之类的事情(例如在 C in 中用来做 int my_count = 0)
【问题讨论】:
-
我在提供的代码中没有看到
logs_count。你的意思是my_count? -
那段代码没问题,不需要在python循环外初始化
my_count
标签: python scope with-statement