-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray_list.py
More file actions
36 lines (29 loc) · 1.27 KB
/
array_list.py
File metadata and controls
36 lines (29 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# module to append multiple python lists to a file.
import sys
import os
import json
def my_array_lists(filename, list_name, arr):
# attempting to read from file.
if os.path.exists(filename) and os.path.getsize(filename) > 0: # if the file exists and if it is not empty.
try:
with open(filename, "r") as file:
data = json.load(file)
except FileNotFoundError or json.JSONDecodeError: # while the file doesn't exist & if the file content
# is not valid JSON...
data = {} # ...initialize our dictionary.
else: # no error code...
data = {} # ...initialize dictionary.
data[list_name] = arr
with open(filename, "w") as file: # write our updated dictionary to the file.
json.dump(data, file, indent=4)
array_elements = input("array list elements (seperated by commas): ")
try:
array_list = [int(x) for x in array_elements.split(",")]
arr = array_list
except ValueError:
print("Enter whole numbers as array elements.")
sys.exit()
filename = "list.json"
list_name = input("name the array list: ")
my_array_lists(filename, list_name, arr)
# print(f"{list_name} : {arr}")