Skip to content

FileHelper (FileMan)

Squid Coder edited this page Nov 10, 2024 · 1 revision

FileHelper Documentation

Overview

The FileHelper class provides utilities for managing file paths and handling file I/O operations. It supports saving and loading files of a specific type (e.g., .txt, .csv) via file dialogs, as well as safely opening files for writing while ensuring that necessary directories are created.

Classes

1. FileHelper

This class contains methods for file operations, including saving data to a file, loading data from a file, and safely opening files for writing.

Constructor:

  • __init__(self): Initializes the FileHelper object and sets up the paths dictionary to store commonly used file paths:

    • base_path: The parent directory of the current script.
    • internals_path: The directory of the current script.
    • lang_path: Placeholder for a language-related directory.

    Example:

    file_helper = FileHelper()
    print(file_helper.paths)

    The above code will print the paths dictionary containing base_path, internals_path, and lang_path.

Methods

1. FileHelper.saveToFiletype

This method allows you to save a list of data to a file of a specified type. A file dialog will prompt you to choose the location and name of the file.

  • Parameters:

    • dataList: A list of data to write to the file.
    • type: The file extension (e.g., txt, csv).
    • typeName: A human-readable name for the file type (e.g., "Text Files").
  • Usage Example:

    file_helper.saveToFiletype(['item1', 'item2', 'item3'], 'txt', 'Text Files')

    This will prompt the user to save the data into a .txt file, where each item from the dataList will be written on a new line.

2. FileHelper.loadFromFiletype

This method allows you to load data from a file of a specified type. It reads the file into a list, where each line of the file becomes an item in the list.

  • Parameters:

    • type: The file extension (e.g., txt, csv).
    • typeName: A human-readable name for the file type (e.g., "Text Files").
  • Returns:

    • A list of strings, where each string represents a line in the file.
  • Usage Example:

    data_list = file_helper.loadFromFiletype('txt', 'Text Files')
    print(data_list)

    This will prompt the user to open a .txt file and return a list of strings, each corresponding to a line in the file.

3. FileHelper.safe_open_w

This method opens a file for writing, ensuring that any necessary parent directories are created before opening the file. By default, it opens the file in write mode (w), but other modes are supported.

  • Parameters:

    • path: The path to the file you want to open.
    • mode (optional): The file mode (default is 'w', i.e., write).
    • newline (optional): The newline character (default is '').
    • encoding (optional): The encoding for the file (default is None).
  • Returns:

    • A file object that can be used to write to the file.
  • Usage Example:

    with file_helper.safe_open_w('path/to/file.txt') as file:
        file.write("Hello, world!")

    This will open file.txt for writing, creating the necessary directories if they don't exist.


Usage Example

Here’s an example of how to use the FileHelper class in your application:

from SquidLibs import FileHelper

# Create a FileHelper instance
file_helper = FileHelper()

# Save a list to a file
file_helper.saveToFiletype(['item1', 'item2', 'item3'], 'txt', 'Text Files')

# Load data from a file
data = file_helper.loadFromFiletype('txt', 'Text Files')
print(data)

# Write data to a file, creating directories if necessary
with file_helper.safe_open_w('path/to/file.txt') as file:
    file.write("Some data")

Clone this wiki locally