S
S
swpavlov2020-08-15 15:28:13
Python
swpavlov, 2020-08-15 15:28:13

How to save an array after a program restart?

There are arrays of photos and names that are replenished with the program, how can you save changes in the arrays after the program is terminated?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
#
# ., 2020-08-15
@andro1

You can use the json module
Write:

import json
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
with open('test.json', 'w') as f:
    f.write(json.dumps(a))

Reading:
import json
with open('test.json', 'r') as f:
    b = json.loads(str(f.read()))
print(b)

You need to write the array to the json file, and then read the array from the file. If anything, json is built-in and you don't need to install it

A
alekssamos, 2020-08-15
@alekssamos

Another pickle option.

import pickle
arr = ["one.jpg", "two.jpg"]
with open('arr.pickle', 'wb') as f:
    pickle.dump(arr, f, 2)

import pickle
with open('arr.pickle', 'rb') as f:
    arr = pickle.load(f)

print(arr)

M
MechanicZelenyy, 2020-08-16
@MechanicZelenyy

Store photos and other data in a database.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question