-
Notifications
You must be signed in to change notification settings - Fork 0
FileHelper (FileMan)
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.
This class contains methods for file operations, including saving data to a file, loading data from a file, and safely opening files for writing.
-
__init__(self): Initializes theFileHelperobject and sets up thepathsdictionary 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
pathsdictionary containingbase_path,internals_path, andlang_path. -
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
.txtfile, where each item from thedataListwill be written on a new line.
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
.txtfile and return a list of strings, each corresponding to a line in the file.
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 isNone).
-
-
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.txtfor writing, creating the necessary directories if they don't exist.
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")