bessagroup / rvesimulator

Automated representative volume element simulator via abaqus for material constitutive law discovery
https://bessagroup.github.io/rvesimulator/
MIT License
14 stars 2 forks source link

Suggestion for using pathlib instead of os.path library #61

Open mpvanderschelling opened 10 months ago

mpvanderschelling commented 10 months ago

This is merely a suggestion, but I think it would be beneficial to use the pathlib library instead of the os.path library. The pathlib library is more modern and has a more object-oriented approach to working with file paths. It also has better support for working with different operating systems.

I have created a code snippet of the same code but with pathlib instead of os.path:

def remove_files(
    directory: str,
    file_types: list = [".log", ".lck", ".SMABulk",
                        ".rec", ".SMAFocus",
                        ".exception", ".simlog", ".023", ".exception"],
) -> None:
    """Remove files of specified types in a directory.

    Parameters
    ----------
    directory : str
        Target folder.
    file_types : list
        List of file extensions to be removed.
    """
    # Create a Path object for the directory
    dir_path = Path(directory)

    for target_file in file_types:
        # Use glob to find files matching the target extension
        target_files = dir_path.glob(f"*{target_file}")

        # Remove the target files if they exist
        for file in target_files:
            if file.is_file():
                file.unlink()

Assignees: Labels:

https://github.com/bessagroup/rvesimulator/blob/ea31752a6da3131cfe55027895ac23ddf684f3de/src/rvesimulator/abaqus2py/abaqus_simulator.py#L367-L403