python json dump writability “not write able”

独自空忆成欢 提交于 2019-12-07 13:55:29

You are not opening your file in "write mode".

Try to change your open() line to this:

with open("/storage/emulated/0/com.hipipal.qpyplus/scripts3/newgame2.txt", "w") as data_file:
    json.dump(hero, data_file)

By default Python's builtin open() opens files in "read" mode.

The most commonly-used values of mode are 'r' for reading, 'w' for writing (truncating the file if it already exists), and 'a' for appending (which on some Unix systems means that all writes append to the end of the file regardless of the current seek position). If mode is omitted, it defaults to 'r'. The default is to use text mode, which may convert '\n' characters to a platform-specific representation on writing and back on reading. Thus, when opening a binary file, you should append 'b' to the mode value to open the file in binary mode, which will improve portability. (Appending 'b' is useful even on systems that don’t treat binary and text files differently, where it serves as documentation.) See below for more possible values of mode.

Change the open line to include 'w' which tells Python to open the file in write mode

 with open("/storage/emulated/0/com.hipipal.qpyplus/scripts3/newgame2.txt", 'w') as data_file:
        json.dump(hero, data_file)

https://docs.python.org/3.4/library/functions.html#open

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!