File Operations Tools

These File Operations Tools offer a streamlined way to manage files and directories in your projects. With functions for saving, reading, listing files, and downloading content from URLs, these tools enhance productivity and simplify file handling in your applications.

save_to_file

Description: Saves the given content to a specified file at a designated path. It ensures that the directory exists before writing the content to the file.

Parameters:

  • content (str): The content to be written to the file.

  • file_name (str): The name of the file to save the content in.

  • file_path (str): The path where the file should be saved.

Returns: None. A message is printed to confirm the file has been saved.

save_to_file
def save_to_file(content: str, file_name: str, file_path: str) -> None:
    """
    Saves the given content to a file at the specified path with the specified file name.

    :param content: The content to be written to the file.
    :param file_name: The name of the file to save the content in.
    :param file_path: The path where the file should be saved.
    """

    # ensure the directory exists
    os.makedirs(file_path, exist_ok=True)

    # construct the full file path
    full_path = os.path.join(file_path, file_name)

    # write the content to the file
    with open(full_path, "w") as file:
        file.write(content)

    print(f"File saved to {full_path}")

read_from_file

Description: Reads the content from a specified file at the given path. It opens the file in read mode and returns its content.

Parameters:

  • file_path (str): The path of the file to read.

Returns:

  • str: The content of the file.

read_from_file
def read_from_file(file_path: str) -> str:
    """
    Reads the content from a file at the specified path.

    :param file_path: The path of the file to read.
    :return: The content of the file.
    """
    with open(file_path, "r") as file:
        return file.read()

list_files

Description: Lists all files in a specified directory. It retrieves the names of all files located in the provided directory path.

Parameters:

  • file_path (str): The path of the directory to list.

Returns:

  • list[str]: A list of file names in the specified directory.

list_files
def list_files(file_path: str) -> list[str]:
    """
    Lists the files in the specified directory.

    :param file_path: The path of the directory to list.
    :return: A list of files in the directory.
    """
    return os.listdir(file_path)

download_from_url

Description: Downloads content from a specified URL and saves it to a file at the given path with the specified file name. If the download is successful, the path to the saved file is returned.

Parameters:

  • url (str): The URL to download the content from.

  • file_name (str): The name of the file to save the downloaded content to.

  • file_path (str): The path where the downloaded content should be saved.

Returns:

  • str: The path to the saved content. Raises an exception if the download fails.

download_from_url
def download_from_url(url: str, file_name: str,file_path: str) -> str:

    """
    Saves the given content to a file at the specified path with the specified file name.

    :param url: Url to download the image from.
    :param file_name: Name of the file to save the image to.
    :param file_path: Path to save the image to.
    :return: Path to the saved image.
    """
    response = requests.get(url)
    if response.status_code == 200:
        # Create the full directory path
        os.makedirs(file_path, exist_ok=True)
        
        full_path = os.path.join(file_path, file_name)
        
        # Write the image content to the file
        with open(full_path, "wb") as f:
            f.write(response.content)
        
        return full_path
    else:
        raise Exception(f"Failed to download image: HTTP {response.status_code}")

Last updated

Logo

© 2024 SwarmZero Technology Solutions Inc. All rights reserved.