从Powershell运行命令

时间:2014-11-11 20:52:46

标签: python

我是Python的初学者,正在通过在线教程学习。在例如15 on this page的Study Drill#6上,我正在尝试打开我创建并存在的'ex15_sample.txt'文件。

这就是它要做的事情:

  

启动'python'启动Python shell,并在提示中使用'open',就像在这个程序中一样。请注意如何打开文件并在'python'中对它们运行'read'?

在命令提示符下,我输入了python来启动Python shell并尝试输入以下内容,这给我一个错误:

open(ex15_sample.txt)

我得到的错误是:

Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'ex15_sample' is not defined

ex15_sample.txt是存在于同一文件夹中的文件。我如何使用open或提示中的任何其他命令?顺便说一句,我在Powershell上运行Python 2.7.8。

1 个答案:

答案 0 :(得分:1)

open的第一个参数是包含文件名的字符串。

只需将文件名放在Quotation Mark

f = open('ex15_sample.txt')
相关问题