目的是查找脚本上方目录中的 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.
发布评论
评论(2)
代码看起来不错,请确保引用的文件位于适当的位置。鉴于您的代码包含目标/文件变量分配,
.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你需要这样做:
因为默认情况下
open
会尝试以读取模式 (r
) 打开文件,但你需要以w
打开它(写入)模式(如果您希望创建它)。另外,我不会使用变量名
file
因为它也是 python 中的一种类型 (
)。您也可以将写入模式标志添加到此行:
因为从我们在问题中掌握的有限信息来看,您的
/.extras
文件以前并不存在。最后注意,您在这一行中丢失了打开文件的句柄(因为您没有将其存储在
file
变量中):完成后您打算如何关闭该文件?
您可以使用上下文管理器(如果使用
import with_statement
,则 python >=2.6 或 2.5)来执行此操作:它将负责为您关闭文件。
You need to do:
because by default
open
will try to open the file in read mode (r
) but you need to open it inw
(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:
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):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):which will take care of closing the file for you.