-
Notifications
You must be signed in to change notification settings - Fork 4
/
FileIO.py
77 lines (56 loc) · 2.23 KB
/
FileIO.py
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import easygui as eg
import pandas as pd
import os
import csv
import time
from datetime import datetime
from stat import S_IREAD, S_IWUSR
####################### FILE IO ###########################
def get_directory(title = "Choose Directory"):
return eg.diropenbox(title)
def write_line_csv(filepath, data_dict):
with open(filepath, 'a', newline = '') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames = list(data_dict.keys()))
if os.stat(filepath).st_size == 0:
writer.writeheader()
writer.writerow(data_dict)
#read into pandas dataframe - works, quick to code
#and is likely easy to extend - but one line doesn't really need it - likely quicker ways to do it
#df = pd.DataFrame(data_dict).T
#save to csv - append, no index, no header
#df.to_csv(filepath, header=False, mode='a', index=False)
def write_line_txt(filepath, line):
with open(filepath, 'a') as txtfile:
txtfile.write(f'{time.time()}: {line}\n')
def start_file(directory, name, extension = '.csv'):
dt = datetime.now().strftime("%Y-%m-%d %H-%M-%S")
filename = f'{name} {dt}{extension}'
filepath = os.path.join(directory, filename)
return filepath
def get_filepath(name = None, mult = False):
if name == None:
title = "Select the file"
else:
title = name
return eg.fileopenbox(title = name, filetypes = [['*.csv', 'CSV Files']], multiple = mult)
def get_multiple_filepaths(name = None):
return get_filepath(name = name, mult = True)
def ensure_subdir_exists_dir(filedir, subdir_name):
candidate_dir = os.path.join(filedir, subdir_name)
if not os.path.exists(candidate_dir):
os.makedirs(candidate_dir)
return candidate_dir
def ensure_subdir_exists_file(filepath, subdir_name):
return ensure_subdir_exists_dir(os.path.dirname(filepath), subdir_name)
def write_data(filepath, data, printout=False, first_line = False):
data["Log_Timestamp"] = time.time()
if(printout):
print(data)
write_line_csv(filepath, data)
def set_read_only(filepath):
#make the file read-only so we don't lose decimal places if the CSV is opened in excel
os.chmod(filepath, S_IREAD)
def allow_write(filepath):
#make the file writable
#https://stackoverflow.com/questions/28492685/change-file-to-read-only-mode-in-python
os.chmod(filepath, S_IWUSR|S_IREAD)