python - JSON transfer not working between processes -


i creating board game, , long story short: want file start , set main variables of game (player name, board(array), prompt, status - playing/gameover) , export these .json file. set file large giu file run main game (logic , display) in separate file.

i practising doing simple tic-tac-toe game, reason cannot json file export/import correctly (can't tell which) or can't input function work in separate function.

(code basic , still incomplete, trying first step work - start up, ask name, run file function display board , ask users next move)

solution 1) using subprocess.popen

file1:

import json, subprocess, os distlib.compat import raw_input   print('welcome tic-tac-toe!') print()  #set of start information going passed json payload , sent each process , forth #------------------------------------------------ name = raw_input('please enter name: ') prompt = 'select space: ' board = [0,1,2,      3,4,5,      6,7,8] move = none status = 'playing' winner = none iter = 0 #------------------------------------------------   #json payload {dictionary} sent info = {'name':name, 'prompt':prompt, 'board':board, 'move':move, 'status': status, 'winner': winner, 'iter': iter, }  print('start, info dictionary goes out: ', info)    #file out dumps info info.json file fout = open('info.json', 'w') json.dump(info, fout) fout.close   subprocess.popen(["python3", "engineandvisuals.py"]) 

file 2) 'engineandvisuals.py'

#imported libraries import random, funcs f, json distlib.compat import raw_input    #front-end: iterface function def interface(jsonfile):     fin = open(jsonfile, 'r')                                                        #open     json file read    info = json.load(fin)                                                             #load json file info    fin.close                                                                        #close json file     print('front, info dictionary comes in: ', info)    print()     name = info['name']                                                                  #set json-name name     prompt = info['prompt']                                                      #set json-prompt prompt       board = info['board']                                                            #set json-board board    winner = info['winner']                                                          #set json-winner winner    status = info['status']                                                          #set json-status status            f.printboard(board)                                                              #prints out board user can see    print(info['move'])    info['move'] = input(f.returnname(name)+', please '+ f.returnprompt(prompt))     print('front, info dictionary goes out: ', info)     fout = open(jsonfile, 'w')                                                       #open json file write    json.dump(info, fout)                                                            #dumps new info file       fout.close                                                                       #closes json file 

the f.functions simple print functions located in file. method getting way input line in second file, program stops. doesn't terminate, cores aren't running hard @ (so don't think it's in loop) stops after program asks space.

method 2) if use os.system instead open process gives me huge error taking mean importing/exporting json file incorrectly.

traceback (most recent call last):   file "engineandvisuals.py", line 105, in <module>     interface('info.json')   file "engineandvisuals.py", line 19, in interface     info = json.load(fin)                                                                #load json file info   file "/usr/lib/python3.4/json/__init__.py", line 268, in load     parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)   file "/usr/lib/python3.4/json/__init__.py", line 318, in loads     return _default_decoder.decode(s)   file "/usr/lib/python3.4/json/decoder.py", line 343, in decode     obj, end = self.raw_decode(s, idx=_w(s, 0).end())   file "/usr/lib/python3.4/json/decoder.py", line 361, in raw_decode     raise valueerror(errmsg("expecting value", s, err.value)) none valueerror: expecting value: line 1 column 1 (char 0) 

i insanely confused because when run second file alone after first works perfectly.

i appreciate help, know it's silly overlook, noob use help, thank much.

short answer : fout.close missing parens - want fout.close() - or better use with statement:

with open('info.json', 'w') fout:     json.dump(info, fout) 

longer answer:

without parens, fout.close evals close method of fout, method not called:

>>> f = open("foo.txt", "w") >>> print f.close <built-in method close of file object @ 0xf0d660> 

since file not closed, buffer not flushed disk, subprocess cannot read it's content.

once main process ends, file object closed @ garbage collection time , buffer flushed disk, if execute second script on it's own @ time read file content.


Comments