使用打开功能。我如何指定路径?
我正在尝试打开我创建的文件,但无法打开。我怀疑打开函数没有使用正确的路径...我该如何放置路径?
>>> filehandler = open(fruits.obj,'w')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'fruits' is not defined
I'm trying to open a file I created and I don´t get it. I suspect that the open function is not using the proper path... How can I put the path?
>>> filehandler = open(fruits.obj,'w')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'fruits' is not defined
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
open
函数采用包含文件路径的字符串作为其第一个参数。在您的例子中,您没有使用字符串,但您告诉 Python 使用fruits
对象的obj
属性。由于没有fruits
对象,您会收到NameError
异常。您可能应该将程序更改为:
The
open
function takes a string containing the path to your file as its first argument. In your case, you didn't use a string, but you told Python to use theobj
property of thefruits
object. As there is nofruits
object, you get aNameError
exception.You should probably change your program to :
使用双引号:
Use double quotes: