0

I want to reload my JSON after my text file is updated by reopening the text file.

import json

with open('np.txt') as np:
    np_data = np.read()
np_list=json.loads(np_data)

def reopen_file():
    print("reloading")
    with open('np.txt') as np:
        np_data = np.read()
    np_list=json.loads(np_data)



y=1
while(y==1):
    #np.flush()
    #reopen_file()
    x=input("input: ")
    print("your input is" +x)
    if(int(x)==1):
        print(np_list)
        y=input("continue?: ")

reopen_file()
print(np_list)

OUTPUT: enter image description here

I update the text file before entering the value for 'y'(continue?), but the output remains the same. (I am editing the file manually)

np.txt:

{"a":"1",
"b":"2",
"c":"3",
"d":"4",
"e":"5",
"f":"6",
"g":"7",
"h":"8",
"i":"9"}
DeadPool
  • 185
  • 2
  • 13
  • 1
    why would the file change? you aren't editing it anywhere. also, no images of code or terminal output! >:( – jemand771 Jun 07 '21 at 10:25
  • @jemand771 I am editing the file manually, not in code – DeadPool Jun 07 '21 at 10:30
  • @jemand771 you are not updating the actual `np_list`, just return it and set it in the loop and it will work. – Sandro Massa Jun 07 '21 at 10:38
  • please check this [one](https://stackoverflow.com/questions/27829575/python-refresh-file-from-disk) – gowridev Jun 07 '21 at 10:39
  • Off-topic: You could read the JSON file in one step using `json.load()` instead of the two steps you're currently doing (i.e. `file.read()` followed by `json.loads()`). – martineau Jun 07 '21 at 11:34

2 Answers2

1

np_list is declared as a global variable that you can't overwrite without using the global keyword. Simplified example:

a = 3
def change_a():
    a = 4
    print("inside:", a)

print("outside:", a)
change_a()
print("outside after:", a)

output:

outside: 3
inside: 4
outside after: 3

example using the global keyword: (don't do this if you have other options like making a class)

a = 3
def change_a():
    global a
    a = 4
    print("inside:", a)

print("outside:", a)
change_a()
print("outside after:", a)

output:

outside: 3
inside: 4
outside after: 4

Another small hint about how you read your json file: instead of doing

import json
with open('np.txt') as np:
    np_data = np.read()
np_list=json.loads(np_data) 

you can just

with open("np.txt") as np:
    np_list = json.load(np)
jemand771
  • 457
  • 1
  • 6
  • 17
  • I am using np_list in other functions. From 2nd hint, won't np_list be inaccessible in other functions coz of it not being a global variable? – DeadPool Jun 07 '21 at 11:01
  • 1
    @DeadPool depends on where you do it. If you just do it in the global scpoe (not inside a function), the `with` statement doesn't matter. Just because it's indented doesn't mean it's a different scope. Of course, if you use this statement inside a function, you'll have to use the `global` keyword again or use the suggestion from the [other answer](https://stackoverflow.com/a/67870169/9145163) – jemand771 Jun 07 '21 at 11:04
1

As I already commented, your code is not actually updating the same np_list you are using. A quick and dirty example to show this is just adding a print(np_list) at the end of reopen_file:

input: 1
your input is1
{'e': '5', 'b': '2', 'd': '4', 'f': '5', 'c': '3', 'a': '1', 'h': '8', 'i': '9', 'g': '7'}
continue?: 1
reloading
{'e': '5', 'b': '2', 'd': '4', 'f': '6', 'c': '3', 'a': '1', 'h': '8', 'i': '9', 'g': '7'}
{'e': '5', 'b': '2', 'd': '4', 'f': '5', 'c': '3', 'a': '1', 'h': '8', 'i': '9', 'g': '7'}

This solution works fine:

import json


def reopen_file():
    with open('np.txt') as np:
        np_data = np.read()
    return json.loads(np_data)


np_list=reopen_file()


y=1
while(y==1):
    #np.flush()
    #reopen_file()
    x=input("input: ")
    print("your input is" +x)
    if(int(x)==1):
        print(np_list)
        y=int(input("continue?: "))
    np_list = reopen_file()

Which outputs:

Python-reload-file> python test.py
input: 1
your input is1
{'d': '4', 'b': '2', 'f': '5', 'h': '8', 'e': '5', 'a': '1', 'c': '3', 'i': '9', 'g': '7'}
continue?: 1
input: 1
your input is1
{'d': '4', 'b': '2', 'f': '6', 'h': '8', 'e': '5', 'a': '1', 'c': '3', 'i': '9', 'g': '7'}
continue?:
Sandro Massa
  • 98
  • 1
  • 10
  • I am testing this out for my app which will have 99% reading and only 1% update. So, I want to update np_list only when I am updating the file once in a while. This same np_list is used in other functions for reading. Will your method automatically update the global np_list which is read in other functions? – DeadPool Jun 07 '21 at 11:04
  • 1
    if you use a global np_list yes. I think you are better off with an object that keeps the information encapsuled and you call an update on it to reload the data. – Sandro Massa Jun 07 '21 at 11:50