Python 以 open() 作为名称 - 名称未定义?
Python with open() as name - name not defined?
我想打开一个包含一列单词的文本文件并创建一个列表,或者创建一个包含这些单词的字符串。为什么会出现此错误:
>>> with open(some_file.txt, 'r') as some_file:
... some_list = [_ for _ in some_file.read().rstrip('\n')]
... print(some_list)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'some_file' is not defined
Python 正在寻找名为 some_file 的对象而不是路径字符串
将some_file.txt
替换为'some_file.txt'
所有问题都在这里回答:
话题可以关闭了。
我想打开一个包含一列单词的文本文件并创建一个列表,或者创建一个包含这些单词的字符串。为什么会出现此错误:
>>> with open(some_file.txt, 'r') as some_file:
... some_list = [_ for _ in some_file.read().rstrip('\n')]
... print(some_list)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'some_file' is not defined
Python 正在寻找名为 some_file 的对象而不是路径字符串
将some_file.txt
替换为'some_file.txt'
所有问题都在这里回答:
话题可以关闭了。