Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 当W+;使用的论点_Python - Fatal编程技术网

Python 当W+;使用的论点

Python 当W+;使用的论点,python,Python,我有 datadir = os.path.dirname(__file__) + '/../some/place' session_file = '/user_session.json' with open(datadir + session_file, 'r') as data_file: data = json.load(data_file) print data 这一点与预期相符。我可以在json文件中加载json并访问它 我想使用w+参数,以便在文件不存在时创

我有

datadir = os.path.dirname(__file__) + '/../some/place'
session_file = '/user_session.json'

with open(datadir + session_file, 'r') as data_file:    
    data = json.load(data_file)
    print data
这一点与预期相符。我可以在json文件中加载json并访问它

我想使用
w+
参数,以便在文件不存在时创建它(尽管为空)

除非我使用
w+
加载失败,出现以下错误,文件被空白文件覆盖

ValueError('No JSON object could be decoded',)

如果文件不在那里,如何创建它,如果在那里,如何读取它,而不会像这样失败?

尝试测试文件是否在那里

import os.path
import os
datadir = os.path.dirname(__file__) + '/../some/place'
session_file = '/user_session.json'

path = datadir + session_file
if os.path.exists(path ):
    open(path, 'w+').close()

with open( path , 'r') as data_file:    
    data = json.load(data_file)
    print data

尝试测试文件是否存在

import os.path
import os
datadir = os.path.dirname(__file__) + '/../some/place'
session_file = '/user_session.json'

path = datadir + session_file
if os.path.exists(path ):
    open(path, 'w+').close()

with open( path , 'r') as data_file:    
    data = json.load(data_file)
    print data

您要检查文件是否存在,并做出相应的反应:

import json
import os.path

datadir = os.path.dirname(__file__)
session_file = 'user_session.json'
path = os.path.join(datadir, '..', 'some', 'place', session_file)

# If the file exists, read the data.
if os.path.exists(path):
    with open(path, 'r') as f:
        data = json.load(f)
        print data
else:
    with open(path, 'w+') as f:
        # Initialize the session file as you see fit.
        # you can't use json.load(f) here as the file was just created,
        # and so it would not decode into JSON, thus raising the same error
        # you are running into.

注意这里使用的
os.path.join
;这是构造文件路径而不是连接字符串的更好方法。本质上,使用
os.path.join
可以确保文件路径仍然包含有效的斜杠,而不管您的操作系统如何。

您想检查文件是否存在并做出相应的反应:

import json
import os.path

datadir = os.path.dirname(__file__)
session_file = 'user_session.json'
path = os.path.join(datadir, '..', 'some', 'place', session_file)

# If the file exists, read the data.
if os.path.exists(path):
    with open(path, 'r') as f:
        data = json.load(f)
        print data
else:
    with open(path, 'w+') as f:
        # Initialize the session file as you see fit.
        # you can't use json.load(f) here as the file was just created,
        # and so it would not decode into JSON, thus raising the same error
        # you are running into.


注意这里使用的
os.path.join
;这是构造文件路径而不是连接字符串的更好方法。本质上,使用
os.path.join
可以确保文件路径仍然包含有效的斜杠,而不管您的操作系统如何。

我不确定创建空文件对您有多大帮助
json.load
在尝试解析空文件时会引发异常,因此您可能应该捕获
FileNotFoundError
(或者Python 2中的
IOError
),然后执行任何适当的操作。是的,我认为这就是发生的情况。也许我应该编写一些空白的DICT作为JSON来初始化文件?问题是,即使文件中有有效的JSON,空白文件也会覆盖好文件。如果文件不存在,你真的需要创建它吗?如果它不在那里读取数据,为什么不将
data
设置为某个适当的值,而不去处理文件呢?我不确定创建一个空文件对您有多大帮助
json.load
在尝试解析空文件时会引发异常,因此您可能应该捕获
FileNotFoundError
(或者Python 2中的
IOError
),然后执行任何适当的操作。是的,我认为这就是发生的情况。也许我应该编写一些空白的DICT作为JSON来初始化文件?问题是,即使文件中有有效的JSON,空白文件也会覆盖好文件。如果文件不存在,你真的需要创建它吗?如果它不在那里读取数据,为什么不将
数据
设置为某个适当的值,而不去处理文件呢?文件在那里是因为它加载了
'r'
。但是我喜欢你的方法:)那不会解决问题——当你调用
json时,读取会失败。加载
值为
w+
@RushyPanchal agree,fixed文件在那里,因为它加载了
'r'
。但是我喜欢你的方法:)那不会解决问题——当你调用
json时,读取会失败。加载
值为
w+
@RushyPanchal agree,修复了路径join上的提示:)虽然我仍然不明白为什么在我的原始示例中,
'w+'
最终会在一个有效文件中被覆盖。
w
表示“打开文件进行写入,删除任何现有内容”<代码>+
表示“还允许读取”。如果要打开文件进行读/写操作并保留内容,请使用
a+/code>。路径join:)上的好提示,尽管我仍然不明白为什么
“w+”
会在我的原始示例中被覆盖的有效文件中结束。
w
表示“打开文件进行写入,删除任何现有内容”<代码>+
表示“还允许读取”。如果要打开文件进行读/写操作并保留内容,请使用
a+