pysoftk.folder_manager package
Submodules
pysoftk.folder_manager.folder_creator module
- class pysoftk.folder_manager.folder_creator.Fld[source]
Bases:
objectA utility class for automated folder and file management.
This class provides methods to create, organize, and manipulate folders and files within the current working directory. It can be used as a stand-alone application or integrated with other Python projects like PySoftK.
- copy_dir(source, destination)[source]
Moves a file or directory from a source path to a destination path.
Note: Despite the name copy_dir, this function performs a move operation, renaming the source to the destination. If you intend to copy, use shutil.copy or shutil.copytree directly.
- Parameters:
source (str) – The path to the file or directory to be moved.
destination (str) – The new path, including the desired new name, for the moved file or directory.
- Returns:
This method moves the specified item and does not return a value.
- Return type:
None
- Raises:
OSError – If the move operation fails (e.g., file not found, permission denied).
- create(times=None, fixed_names=None)[source]
Creates one or more folders in the current working directory.
You can either specify the number of folders to create (which will be assigned unique random names) or provide a pre-defined array of names.
- Parameters:
times (int, optional) – The number of folders to create. If provided, unique random names will be generated for each folder. Cannot be used with fixed_names.
fixed_names (np.ndarray, optional) – An array of strings, where each string is a desired name for a folder. If provided, times will be ignored. Cannot be used with times.
- Returns:
This method creates folders and prints a success message to the console.
- Return type:
None
- Raises:
ValueError – If neither times nor fixed_names is provided, or if both are provided.
- file_to_dir(format_extension, num_cores=None, fixed_names=None)[source]
Moves files with a specific extension into newly created or existing directories in a parallelized manner.
This method first identifies all files matching the given format_extension. Then, it either creates new uniquely named folders (if fixed_names is not provided) or uses the fixed_names to create folders. Finally, it moves each identified file into a corresponding folder, distributing the work across multiple CPU cores for efficiency.
- Parameters:
format_extension (str) – The extension of the files to be moved (e.g., “txt”, “jpg”).
num_cores (int, optional) – The number of CPU cores to utilize for parallel processing. If None, the process will default to 1 core (sequential execution).
fixed_names (np.ndarray, optional) – An array of strings to use as names for the newly created folders. If provided, the number of folders created will match the length of this array. If None, folders will be created with unique random names, matching the number of files found.
- Returns:
This method moves files and prints a message indicating completion.
- Return type:
None
- Raises:
NotImplementedError – This error is currently listed in the docstring but not explicitly raised by the code. It might be a placeholder for future validation or error handling related to folder creation issues.
- fxd_name(testname, times)[source]
Generates an array of sequentially numbered folder names based on a base name.
This method is useful for creating a series of folders with a consistent naming convention, such as ‘experiment_1’, ‘experiment_2’, etc.
- Parameters:
testname (str) – The base name to be used for the folders (e.g., “my_experiment”).
times (int) – The number of folder names to generate.
- Returns:
An array of strings, where each string is a unique folder name (e.g., [“my_experiment_0”, “my_experiment_1”, …]).
- Return type:
np.ndarray
- move_files_to_folder(file_extension, folder_name, num_cores=None)[source]
Moves all files with a specified extension from the current directory into a single, newly created or existing folder. The operation can be parallelized across multiple CPU cores.
- Parameters:
file_extension (str) – The extension of the files to move (e.g., “txt”, “csv”, “pdf”). The leading dot is optional (e.g., “txt” and “.txt” are both valid).
folder_name (str) – The name of the destination folder. If the folder does not exist, it will be created.
num_cores (int, optional) – The number of CPU cores to use for parallel processing. If None, the function will automatically detect and use the number of available cores.
- Returns:
This method performs file movements and does not return a value.
- Return type:
None
- seek_files(format_extension)[source]
Searches for files with a specific extension in the current working directory.
- Parameters:
format_extension (str) – The file extension to search for (e.g., “txt”, “csv”, “pdf”). The leading dot is optional (e.g., both “txt” and “.txt” are valid).
- Returns:
A list of pathlib.Path objects, each representing a file that matches the specified extension in the current directory.
- Return type:
list[pathlib.Path]