具有最少绒毛变量的 Python open()

发布于 2024-12-03 08:29:30 字数 1111 浏览 0 评论 0 原文

目的是查找脚本上方目录中的 json 文件并加载在该文件中找到的内容。这就是我所得到的:

import os
import json

settings_file = '/home/me/foo/bar.txt'
root = os.path.dirname(os.path.dirname(os.path.abspath(settings_file))) # '/home/me'
target = os.path.join(root,'.extras.txt') # '/home/me/.extras.txt'
db_file= open(target)

databases = json.load(db_file) # works, returns object
databases2 = json.load(open(target)) # equivalent to above, also works
# try to condense code, lose pointless variables target and file
databases3 = json.load(open(os.path.join(root,'.extras.txt'))) # equivalent (I thought!) to above, doesn't work.

那么...为什么一次性、无保持变量版本不起作用?哦,返回的错误是(现在是完整的):

$ ./json_test.py
Traceback (most recent call last):
  File "./json_test.py", line 69, in <module>
   databases =  json.load(open(os.path.join(root,'/.extras.txt')))
IOError: [Errno 2] No such file or directory: '/.extras.txt'

并且为了满足 S.Lott 的善意建议...... target 设置为什么并不重要。数据库和数据库2 正确填充,而数据库3 则不正确。 target 存在、可读并且包含 json 期望看到的内容。我怀疑我不明白将命令串在一起的本质...我可以使代码工作,只是想知道为什么简洁(或复杂?)版本失败了。

The intent is to look in a json file in the directory above the script and load up what it finds in that file. This is what I've got:

import os
import json

settings_file = '/home/me/foo/bar.txt'
root = os.path.dirname(os.path.dirname(os.path.abspath(settings_file))) # '/home/me'
target = os.path.join(root,'.extras.txt') # '/home/me/.extras.txt'
db_file= open(target)

databases = json.load(db_file) # works, returns object
databases2 = json.load(open(target)) # equivalent to above, also works
# try to condense code, lose pointless variables target and file
databases3 = json.load(open(os.path.join(root,'.extras.txt'))) # equivalent (I thought!) to above, doesn't work.

So... why doesn't the all-at-once, no holding variables version work? Oh, the error returned is (now in it's entirety):

$ ./json_test.py
Traceback (most recent call last):
  File "./json_test.py", line 69, in <module>
   databases =  json.load(open(os.path.join(root,'/.extras.txt')))
IOError: [Errno 2] No such file or directory: '/.extras.txt'

And to satisfy S.Lott's well-intentioned advice... it doesn't matter what target is set to. The databases and databases2 populate correctly while databases3 does not. target exists, is readable and contains what json expects to see. I suspect there's something I don't understand about the nature of stringing commands together... I can make the code work, was just wondering why the concise (or complex?) version failed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

凑诗 2024-12-10 08:29:30

代码看起来不错,请确保引用的文件位于适当的位置。鉴于您的代码包含目标/文件变量分配,.extras.txt 的完整路径为

/home/me/.extras.txt

Code looks fine, make sure referenced files are in the appropriate places. Given your code that includes target/file variable assignment, full path to .extras.txt is

/home/me/.extras.txt

寂寞美少年 2024-12-10 08:29:30

你需要这样做:

file = open(target, 'w')

因为默认情况下 open 会尝试以读取模式 (r) 打开文件,但你需要以 w 打开它(写入)模式(如果您希望创建它)。

另外,我不会使用变量名 file 因为它也是 python 中的一种类型 ()。

您也可以将写入模式标志添加到此行:

databases = json.load(open(os.path.join(root,'.extras.txt'), 'w'))

因为从我们在问题中掌握的有限信息来看,您的 /.extras 文件以前并不存在。

最后注意,您在这一行中丢失了打开文件的句柄(因为您没有将其存储在 file 变量中):

databases = json.load(open(os.path.join(root,'.extras.txt')))

完成后您打算如何关闭该文件?

您可以使用上下文管理器(如果使用 import with_statement,则 python >=2.6 或 2.5)来执行此操作:

with open(os.path.join(root,'.extras.txt'), 'w') as f:
   databases = json.load(f)

它将负责为您关闭文件。

You need to do:

file = open(target, 'w')

because by default open will try to open the file in read mode (r) but you need to open it in w (write) mode if you want it to be created.

Also, I would not use the variable name file since it is also a type (<type 'file'>) in python.

You could add the write-mode flag to this line as well:

databases = json.load(open(os.path.join(root,'.extras.txt'), 'w'))

because from the limited information we have in the question it appears your /.extras file does not previously exist.

Final note, you are losing the handle to your open file in this line (since you are not storing it in your file variable):

databases = json.load(open(os.path.join(root,'.extras.txt')))

How do you intend to close the file when you're finished with it?

You could do this with a context manager (python >=2.6 or 2.5 if import with_statement used):

with open(os.path.join(root,'.extras.txt'), 'w') as f:
   databases = json.load(f)

which will take care of closing the file for you.

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