114 lines
3.9 KiB
Python
114 lines
3.9 KiB
Python
import os
|
|
import xmltodict
|
|
|
|
def extract_file_details(file_path):
|
|
"""
|
|
Extracts and returns the file name without extension, the file extension, and the file path.
|
|
|
|
Parameters:
|
|
file_path (str): The full path to the file.
|
|
|
|
Returns:
|
|
tuple: (file_name_without_extension, file_extension, path_only)
|
|
|
|
Uso:
|
|
file_name, extension, path = extract_file_details(file_path)
|
|
"""
|
|
# Extrae el path completo del directorio
|
|
path_only = os.path.dirname(file_path)
|
|
|
|
# Extrae el nombre completo del archivo con extension
|
|
full_file_name = os.path.basename(file_path)
|
|
|
|
# Separa la extension del nombre del archivo
|
|
file_name_without_extension, file_extension = os.path.splitext(full_file_name)
|
|
|
|
return (file_name_without_extension, file_extension, path_only)
|
|
|
|
def create_directory(base_path, additional_path=""):
|
|
"""
|
|
Creates a directory at the specified base path, optionally extended by an additional path.
|
|
This function handles paths whether or not they end with a slash.
|
|
|
|
Parameters:
|
|
base_path (str): The base path of the directory to create.
|
|
additional_path (str, optional): Additional path elements to be appended. Default is empty.
|
|
|
|
Returns:
|
|
bool: True if the directory was created successfully, False otherwise.
|
|
"""
|
|
# Construye el path completo asegurando que los componentes están correctamente separados
|
|
full_path = os.path.join(base_path, additional_path)
|
|
|
|
try:
|
|
# Intenta crear el directorio, incluyendo todos los directorios intermedios necesarios
|
|
os.makedirs(full_path, exist_ok=True)
|
|
print(f"Directory '{full_path}' created successfully.")
|
|
return full_path
|
|
except Exception as e:
|
|
# Captura cualquier error que ocurra y lo muestra
|
|
print(f"Failed to create directory '{full_path}'. Error: {e}")
|
|
return full_path
|
|
|
|
def build_file_path(base_path, file_name, extension):
|
|
"""
|
|
Constructs a complete file path given a base path, a file name, and an extension.
|
|
Handles cases where the file name may include an incorrect or correct extension.
|
|
|
|
Parameters:
|
|
base_path (str): The base directory path where the file will be located.
|
|
file_name (str): The file name, which may or may not include an extension.
|
|
extension (str): The desired file extension (e.g., 'txt', '.txt').
|
|
|
|
Returns:
|
|
str: The complete file path including the base path, file name, and extension.
|
|
"""
|
|
# Ensure the extension is in the correct format (i.e., starts with a dot)
|
|
if not extension.startswith('.'):
|
|
extension = '.' + extension
|
|
|
|
# Separate the base file name from its extension if present
|
|
file_name_without_extension, _ = os.path.splitext(file_name)
|
|
|
|
# Reconstruct the file name with the correct extension
|
|
file_name_corrected = file_name_without_extension + extension
|
|
|
|
# Construct the full file path
|
|
full_path = os.path.join(base_path, file_name_corrected)
|
|
|
|
return full_path
|
|
|
|
import json
|
|
|
|
def save_data_as_json(data ,file_path):
|
|
with open(file_path, 'w', encoding='utf-8') as f:
|
|
json.dump(data, f, ensure_ascii=False, indent=4)
|
|
|
|
|
|
def save_json_to_xml(json_data, filename="DB_Structure.xml"):
|
|
"""
|
|
Convert JSON data to XML and save it to a file.
|
|
"""
|
|
xml_data = xmltodict.unparse({"root": json_data}, pretty=True)
|
|
with open(filename, "w") as xml_file:
|
|
xml_file.write(xml_data)
|
|
print(f"XML data saved to {filename}")
|
|
|
|
|
|
def save_dataframe_to_excel(df, filename="DB_Structure.xlsx", sheet_name= "DB"):
|
|
"""
|
|
Save the provided DataFrame to an Excel file.
|
|
"""
|
|
df.to_excel(filename, index=False, sheet_name=sheet_name)
|
|
print(f"Data saved to {filename}")
|
|
|
|
|
|
def save_dataframe_to_file(df, filename="DB_Structure.csv"):
|
|
"""
|
|
Save the provided DataFrame to a CSV file.
|
|
"""
|
|
df.to_csv(filename, index=False)
|
|
print(f"Data saved to {filename}")
|
|
|
|
|
|
|