PyLibrary/funciones_comunes/manejoArchivos.py

50 lines
1.7 KiB
Python
Raw Permalink Normal View History

2024-10-08 12:34:42 -03:00
import pandas as pd
import tkinter as tk
from tkinter import filedialog
import os
import subprocess
def select_file(extension = "txt"):
"""
Opens a file dialog to select a .db file and returns the selected file path.
"""
root = tk.Tk()
root.withdraw() # Use to hide the tkinter root window
# Open file dialog and return the selected file path
file_path = filedialog.askopenfilename(
title=f"Select a .{extension} file",
filetypes=((f"{extension} files", f"*.{extension}"), ("All files", "*.*"))
)
return file_path
def open_file_explorer(path):
"""
Opens the file explorer at the given path, correctly handling paths with spaces.
"""
# Normalize the path to ensure it's in the correct format
normalized_path = os.path.normpath(path)
# Check if the path is a directory or a file and format the command accordingly
if os.path.isdir(normalized_path):
# If it's a directory, use the 'explorer' command directly
command = f'explorer "{normalized_path}"'
else:
# If it's a file, use the 'explorer /select,' command to highlight the file in its folder
command = f'explorer /select,"{normalized_path}"'
# Execute the command using subprocess.run, with shell=True to handle paths with spaces correctly
subprocess.run(command, shell=True)
def select_directory():
"""
Opens a file dialog to select a directory and returns the selected directory path.
"""
root = tk.Tk()
root.withdraw() # Use to hide the tkinter root window
# Open directory dialog and return the selected directory path
directory_path = filedialog.askdirectory(
title="Select a directory"
)
return directory_path