DBsExcel_con_UDT/manejoArchivos.py

37 lines
1.3 KiB
Python
Raw Normal View History

import pandas as pd
import tkinter as tk
from tkinter import filedialog
import os
import subprocess
def select_file():
"""
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="Select a .db file",
filetypes=(("DB files", "*.db"), ("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)