Skip to content

Reference

dsg_lib.common_functions.folder_functions

This module contains functions for working with directories and files.

Functions:

Name Description
last_data_files_changed

Get the last modified file in a

get_directory_list

Get a list of directories in the

specified directory. make_folder

Make a folder in a

specific directory. remove_folder

Remove a folder from the

Example:

from dsg_lib.common_functions import folder_functions

# Get the last modified file in a directory time_stamp, file_path =
folder_functions.last_data_files_changed("/path/to/directory")  # Returns:
(datetime.datetime(2022, 1, 1, 12, 0, 0), '/path/to/directory/test.txt')

# Get a list of directories in the specified directory directories =
folder_functions.get_directory_list("/path/to/directory")  # Returns:
['/path/to/directory/dir1', '/path/to/directory/dir2']

# Make a folder in a specific directory
folder_functions.make_folder("/path/to/directory/new_folder")  # Creates a new
folder at '/path/to/directory/new_folder'

# Remove a folder from the specified directory
folder_functions.remove_folder("/path/to/directory/old_folder")  # Removes the
folder at '/path/to/directory/old_folder'

get_directory_list(file_directory)

Get a list of directories in the specified directory.

Parameters:

Name Type Description Default
file_directory str

The path of the directory to check.

required

Returns:

Type Description
List[str]

List[str]: A list of directories in the specified directory.

Raises:

Type Description
FileNotFoundError

If the directory does not exist.

Example:

from dsg_lib import file_functions

directories = file_functions.get_directory_list("/path/to/directory")

# Returns: ['/path/to/directory/dir1', '/path/to/directory/dir2']

Source code in dsg_lib/common_functions/folder_functions.py
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
def get_directory_list(file_directory: str) -> List[str]:
    """
    Get a list of directories in the specified directory.

    Args:
        file_directory (str): The path of the directory to check.

    Returns:
        List[str]: A list of directories in the specified directory.

    Raises:
        FileNotFoundError: If the directory does not exist.

    Example:
    ```python
    from dsg_lib import file_functions

    directories = file_functions.get_directory_list("/path/to/directory")

    # Returns: ['/path/to/directory/dir1', '/path/to/directory/dir2']
    ```
    """
    # Create a Path object for the specified directory
    file_path = Path.cwd().joinpath(file_directory)

    try:
        # Use a list comprehension to create a list of directories in the
        # specified directory
        direct_list = [x for x in file_path.iterdir() if x.is_dir()]

        # Log a message indicating that the list of directories was retrieved
        logger.info(f'Retrieved list of directories: {file_directory}')

        # Return the list of directories
        return direct_list

    except FileNotFoundError as err:
        # Log an error message if the specified directory does not exist
        logger.error(err)

last_data_files_changed(directory_path)

Get the last modified file in a directory and return its modification time and path.

Parameters:

Name Type Description Default
directory_path str

The path of the directory to check.

required

Returns:

Type Description
datetime

Tuple[datetime, str]: A tuple containing the modification time and path

str

of the last modified file.

Raises:

Type Description
FileNotFoundError

If the directory does not exist.

Example:

from dsg_lib import file_functions

time_stamp, file_path = file_functions.last_data_files_changed("/path/to/directory")

# Returns: (datetime.datetime(2022, 1, 1, 12, 0, 0), '/path/to/directory/test.txt')

Source code in dsg_lib/common_functions/folder_functions.py
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
def last_data_files_changed(directory_path: str) -> Tuple[datetime, str]:
    """
    Get the last modified file in a directory and return its modification time
    and path.

    Args:
        directory_path (str): The path of the directory to check.

    Returns:
        Tuple[datetime, str]: A tuple containing the modification time and path
        of the last modified file.

    Raises:
        FileNotFoundError: If the directory does not exist.

    Example:
    ```python
    from dsg_lib import file_functions

    time_stamp, file_path = file_functions.last_data_files_changed("/path/to/directory")

    # Returns: (datetime.datetime(2022, 1, 1, 12, 0, 0), '/path/to/directory/test.txt')
    ```
    """
    try:
        # Use a generator expression to find the last modified file in the
        # directory
        time, file_path = max((f.stat().st_mtime, f) for f in directory_path.iterdir())

        # Convert the modification time to a datetime object
        time_stamp = datetime.fromtimestamp(time)

        # Log a message to indicate that the directory was checked for the last
        # modified file
        logger.info(f'Directory checked for last change: {directory_path}')

        # Return the modification time and path of the last modified file
        return time_stamp, file_path

    except Exception as err:
        # Log an error message if an exception occurs, and return a default
        # value to indicate an error
        logger.error(err)
        return None, None

remove_folder(file_directory)

Remove a folder from the specified directory.

Parameters:

Name Type Description Default
file_directory str

The directory containing the folder to be removed.

required

Returns:

Type Description
None

None.

Raises:

Type Description
FileNotFoundError

If the specified directory does not exist. OSError:

Example:

from dsg_lib.common_functions import file_functions

file_functions.remove_folder("/path/to/directory/old_folder")

# Removes the folder at '/path/to/directory/old_folder'

Source code in dsg_lib/common_functions/folder_functions.py
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
def remove_folder(file_directory: str) -> None:
    """
    Remove a folder from the specified directory.

    Args:
        file_directory (str): The directory containing the folder to be removed.

    Returns:
        None.

    Raises:
        FileNotFoundError: If the specified directory does not exist. OSError:
        If the specified folder could not be removed.

    Example:
    ```python
    from dsg_lib.common_functions import file_functions

    file_functions.remove_folder("/path/to/directory/old_folder")

    # Removes the folder at '/path/to/directory/old_folder'
    ```
    """
    try:
        # Create a Path object for the specified directory
        path = Path(file_directory)

        # Use the rmdir method of the Path object to remove the folder
        path.rmdir()

        # Log a message indicating that the folder was removed
        logger.info(f'Folder removed: {file_directory}')

    except FileNotFoundError as err:
        # Log an error message if the specified directory does not exist
        logger.error(err)

        # Raise the FileNotFoundError exception to be handled by the calling
        # code
        raise

    except OSError as err:
        # Log an error message if the folder could not be removed
        logger.error(err)

        # Raise the OSError exception to be handled by the calling code
        raise