-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfileOperations.py
More file actions
32 lines (21 loc) · 961 Bytes
/
fileOperations.py
File metadata and controls
32 lines (21 loc) · 961 Bytes
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
import json
# this module includes operations to write to files, open files, return the length of files, and clear files
def write(new_data, filename): # function that appends data to the specified file
with open(filename, "r") as file1:
data = json.load(file1)
data.append(new_data)
with open(filename, "w") as file1:
# Sets file's current position at offset.
file1.seek(0)
json.dump(data, file1, indent=4)
def clear(file): # function to clear the file so appended data isn't constantly repeated
dict1 = [] # After everything is cleared this is what will be put back into the fresh file
out_file = open(file, "w")
json.dump(dict1, out_file, indent=6)
out_file.close()
def file(x): # Function to load specified file
with open(x, 'r+') as file:
return json.load(file)
def length(data): # returns the length of provided file
# data = data_file()
return len(data)