import pandas as pd
from pathlib import PurePath
import os
import sys
import time
[docs]def get_file_datetime(list_of_files):
"""
Parameters:
list_of_files: List; the files for which datetimes are required.
Returns:
A DataFrame of input files (full path) and corresponding datetimes (date stamps) for those files.
"""
file_datetime = pd.DataFrame()
file_datetime.insert(0, 'Item', [path_to_file for path_to_file in list_of_files])
file_datetime.insert(1, 'Results', [time.ctime(os.path.getmtime(path_to_file)) for path_to_file in list_of_files])
return file_datetime
[docs]def save_dict(dict_to_save, save_path, row_header=None, stamp=None, index=False):
"""
Parameters:
dict_to_save: Dictionary; the dictionary to be saved to CSV.\n
save_path: Path object; the path for saving the passed dict_to_save.\n
row_header: List; the column names to use as the row header for the preferred structure of the output file.\n
stamp: str; an identifier for inclusion in the filename, e.g., datetime stamp.\n
index: Boolean; True includes the index; False excludes the index.
Returns:
Saves the passed dictionary to a CSV file.
"""
print('Saving dictionary to CSV.')
df = pd.DataFrame(dict_to_save).transpose()
if row_header:
cols = [col for col in df.columns if col not in row_header]
df = pd.DataFrame(df, columns=row_header + cols)
df.to_csv(f'{save_path}_{stamp}.csv', index=index)
return
[docs]def save_dict_return_df(dict_to_save, save_path, row_header=None, stamp=None, index=False):
"""
Parameters:
dict_to_save: Dictionary; the dictionary to be saved to CSV.\n
save_path: Path object; the path for saving the passed dict_to_save.\n
row_header: List; the column names to use as the row header for the preferred structure of the output file.\n
stamp: str; an identifier for inclusion in the filename, e.g., datetime stamp.\n
index: Boolean; True includes the index; False excludes the index.
Returns:
Saves the passed dictionary to a CSV file and returns a DataFrame based on the passed dictionary.
"""
print('Saving dictionary to CSV.')
df = pd.DataFrame(dict_to_save).transpose()
if row_header:
cols = [col for col in df.columns if col not in row_header]
df = pd.DataFrame(df, columns=row_header + cols)
df.to_csv(f'{save_path}_{stamp}.csv', index=index)
return df