使用打开功能。我如何指定路径?

发布于 2024-10-09 18:24:05 字数 252 浏览 0 评论 0原文

我正在尝试打开我创建的文件,但无法打开。我怀疑打开函数没有使用正确的路径...我该如何放置路径?

>>> 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱你不解释 2024-10-16 18:24:05

open 函数采用包含文件路径的字符串作为其第一个参数。在您的例子中,您没有使用字符串,但您告诉 Python 使用 fruits 对象的 obj 属性。由于没有 fruits 对象,您会收到 NameError 异常。

您可能应该将程序更改为:

>>> filehandler = open("fruits.obj", "w")

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 the obj property of the fruits object. As there is no fruits object, you get a NameError exception.

You should probably change your program to :

>>> filehandler = open("fruits.obj", "w")
苏大泽ㄣ 2024-10-16 18:24:05

使用双引号:

>>> filehandler = open("fruits.obj",'w')

Use double quotes:

>>> filehandler = open("fruits.obj",'w')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文