MenuBase/utils/file_utils.py

39 lines
907 B
Python

# utils/file_utils.py
"""
File handling utilities
"""
import os
import tkinter as tk
from tkinter import filedialog
import pandas as pd
def select_file(title="Select file", filetypes=None):
if filetypes is None:
filetypes = [
("Excel files", "*.xlsx;*.xls"),
("All files", "*.*")
]
root = tk.Tk()
root.withdraw()
file_path = filedialog.askopenfilename(
title=title,
filetypes=filetypes
)
return file_path if file_path else None
def select_directory(title="Select directory"):
root = tk.Tk()
root.withdraw()
dir_path = filedialog.askdirectory(title=title)
return dir_path if dir_path else None
def safe_read_excel(file_path, **kwargs):
try:
return pd.read_excel(file_path, **kwargs)
except Exception as e:
print(f"Error reading Excel file: {e}")
return None