cornsnake.util_dir

Working with directories, files, and file paths.

Documentation

  1"""
  2Working with directories, files, and file paths.
  3
  4[Documentation](http://docs.mrseanryan.cornsnake.s3-website-eu-west-1.amazonaws.com/cornsnake/util_dir.html)
  5"""
  6
  7import os
  8import re
  9import shutil
 10from pathlib import Path
 11
 12from . import util_os
 13
 14TOTAL_BYTES_IN_GIGABYTE = 1000000000
 15
 16
 17def copy_directory(from_path, to_path):
 18    """
 19    Copy a directory from one location to another.
 20
 21    Args:
 22    from_path (str): The path of the directory to copy from.
 23    to_path (str): The path of the directory to copy to.
 24    """
 25    shutil.copytree(from_path, to_path)
 26
 27
 28def ensure_dir_exists(temp_git_fixer_dir):
 29    """
 30    Ensure that a directory exists, creating it if necessary.
 31
 32    Args:
 33    temp_git_fixer_dir (str): The path of the directory to ensure existence of.
 34    """
 35    if not os.path.exists(temp_git_fixer_dir):
 36        os.makedirs(temp_git_fixer_dir)
 37
 38
 39def find_files_by_extension(dir_path, extension):
 40    """
 41    Find files in a directory by a specific file extension.
 42
 43    Args:
 44    dir_path (str): The path of the directory to search for files.
 45    extension (str): The file extension to search for.
 46
 47    Returns:
 48    list: A list of file paths with the specified extension.
 49    """
 50    found_files = []
 51    contents = os.listdir(dir_path)
 52    for content in contents:
 53        path_to_sub = os.path.join(dir_path, content)
 54        if os.path.isfile(path_to_sub) and path_to_sub.endswith(extension):
 55            found_files.append(path_to_sub)
 56    return found_files
 57
 58
 59def find_files(dir_path):
 60    """Find all files in the given directory."""
 61    found_files = []
 62    contents = os.listdir(dir_path)
 63    for content in contents:
 64        path_to_sub = os.path.join(dir_path, content)
 65        if os.path.isfile(path_to_sub):
 66            found_files.append(path_to_sub)
 67    return found_files
 68
 69
 70def get_dir_parts(path_to_file):
 71    """
 72    Get the directory components of the given file path.
 73
 74    Example: 'c:\\temp\\x\\my-file.txt' -> ['c','temp','x']
 75    """
 76    path_to_dir = os.path.dirname(path_to_file)
 77    path = os.path.normpath(path_to_dir)
 78    return path.split(os.sep)
 79
 80
 81def get_directory_of_this_script(____file__):
 82    """
 83    Get the directory that contains this script.
 84
 85    Args:
 86    __file__ (str): The path to this Python script file.
 87    Returns:
 88    The absolute path to directory containing your Python script file.
 89    """
 90    return os.path.dirname(os.path.realpath(____file__))
 91
 92
 93def get_parent_dir(my_path):
 94    """Get the absolute path of the parent directory of the given directory."""
 95    return Path(my_path).parent.absolute()
 96
 97
 98def get_total_dir_size_in_bytes(start_path):
 99    total_size = 0
100    for dirpath, _dirnames, filenames in os.walk(start_path):
101        for f in filenames:
102            fp = os.path.join(dirpath, f)
103            # skip if it is symbolic link
104            if not os.path.islink(fp):
105                fp_allow_long_path = "\\\\?\\" + fp if util_os.is_windows() else fp
106                total_size += os.path.getsize(fp_allow_long_path)
107
108    return total_size
109
110
111def get_total_dir_size_in_gigabytes(start_path):
112    """Calculate the total size of a directory in gigabytes.
113    Args:
114    start_path (str): The path of the directory to calculate size of.
115    Returns:
116    float: The total size of the directory in gigabytes."""
117    return get_total_dir_size_in_bytes(start_path) / TOTAL_BYTES_IN_GIGABYTE
118
119
120def get_unique_dirpath(path_to_dir):
121    """
122    Get a unique directory path similar to the given path.
123    """
124
125    def _ends_with_hyphen_number(path):
126        m = re.search(r"-\d+$", path)
127        return m is not None
128
129    # Avoid appending like x-01-02-03
130    if _ends_with_hyphen_number(path_to_dir):
131        path_to_dir = path_to_dir[:-3]
132
133    suffix = 2
134    path_to_dir_new = path_to_dir
135    while os.path.exists(path_to_dir_new):
136        path_to_dir_new = f"{path_to_dir}-{suffix:02}"
137        suffix += 1
138    return path_to_dir_new
139
140
141def is_empty_directory(path_to_file):
142    """Check if a directory is empty.
143    Args:
144    path_to_file (str): The path of the directory to check.
145    Returns:
146    bool: True if the directory is empty, False otherwise."""
147    if os.path.isfile(path_to_file):
148        return False
149    contents = os.listdir(path_to_file)
150    return len(contents) == 0
151
152
153def is_empty_directory_only_subdirectories(path_to_file):
154    """Check if a directory is empty by inspecting subdirectories.
155    Args:
156    path_to_file (str): The path of the directory to check.
157    Returns:
158    bool: True if the directory is empty or only contains empty subdirectories, False otherwise."""
159    if os.path.isfile(path_to_file):
160        return False
161    contents = os.listdir(path_to_file)
162    for content in contents:
163        path_to_sub = os.path.join(path_to_file, content)
164        if os.path.isfile(path_to_sub):
165            return False
166        if not os.path.isfile(path_to_sub):
167            is_empty = is_empty_directory_only_subdirectories(path_to_sub)
168            if not is_empty:
169                return False
170    return True
171
172
173def move_directory(path_to_dir, path_to_dir_renamed):
174    shutil.move(path_to_dir, path_to_dir_renamed)
TOTAL_BYTES_IN_GIGABYTE = 1000000000
def copy_directory(from_path, to_path):
18def copy_directory(from_path, to_path):
19    """
20    Copy a directory from one location to another.
21
22    Args:
23    from_path (str): The path of the directory to copy from.
24    to_path (str): The path of the directory to copy to.
25    """
26    shutil.copytree(from_path, to_path)

Copy a directory from one location to another.

Args: from_path (str): The path of the directory to copy from. to_path (str): The path of the directory to copy to.

def ensure_dir_exists(temp_git_fixer_dir):
29def ensure_dir_exists(temp_git_fixer_dir):
30    """
31    Ensure that a directory exists, creating it if necessary.
32
33    Args:
34    temp_git_fixer_dir (str): The path of the directory to ensure existence of.
35    """
36    if not os.path.exists(temp_git_fixer_dir):
37        os.makedirs(temp_git_fixer_dir)

Ensure that a directory exists, creating it if necessary.

Args: temp_git_fixer_dir (str): The path of the directory to ensure existence of.

def find_files_by_extension(dir_path, extension):
40def find_files_by_extension(dir_path, extension):
41    """
42    Find files in a directory by a specific file extension.
43
44    Args:
45    dir_path (str): The path of the directory to search for files.
46    extension (str): The file extension to search for.
47
48    Returns:
49    list: A list of file paths with the specified extension.
50    """
51    found_files = []
52    contents = os.listdir(dir_path)
53    for content in contents:
54        path_to_sub = os.path.join(dir_path, content)
55        if os.path.isfile(path_to_sub) and path_to_sub.endswith(extension):
56            found_files.append(path_to_sub)
57    return found_files

Find files in a directory by a specific file extension.

Args: dir_path (str): The path of the directory to search for files. extension (str): The file extension to search for.

Returns: list: A list of file paths with the specified extension.

def find_files(dir_path):
60def find_files(dir_path):
61    """Find all files in the given directory."""
62    found_files = []
63    contents = os.listdir(dir_path)
64    for content in contents:
65        path_to_sub = os.path.join(dir_path, content)
66        if os.path.isfile(path_to_sub):
67            found_files.append(path_to_sub)
68    return found_files

Find all files in the given directory.

def get_dir_parts(path_to_file):
71def get_dir_parts(path_to_file):
72    """
73    Get the directory components of the given file path.
74
75    Example: 'c:\\temp\\x\\my-file.txt' -> ['c','temp','x']
76    """
77    path_to_dir = os.path.dirname(path_to_file)
78    path = os.path.normpath(path_to_dir)
79    return path.split(os.sep)

Get the directory components of the given file path.

Example: 'c:\temp\x\my-file.txt' -> ['c','temp','x']

def get_directory_of_this_script(____file__):
82def get_directory_of_this_script(____file__):
83    """
84    Get the directory that contains this script.
85
86    Args:
87    __file__ (str): The path to this Python script file.
88    Returns:
89    The absolute path to directory containing your Python script file.
90    """
91    return os.path.dirname(os.path.realpath(____file__))

Get the directory that contains this script.

Args: __file__ (str): The path to this Python script file. Returns: The absolute path to directory containing your Python script file.

def get_parent_dir(my_path):
94def get_parent_dir(my_path):
95    """Get the absolute path of the parent directory of the given directory."""
96    return Path(my_path).parent.absolute()

Get the absolute path of the parent directory of the given directory.

def get_total_dir_size_in_bytes(start_path):
 99def get_total_dir_size_in_bytes(start_path):
100    total_size = 0
101    for dirpath, _dirnames, filenames in os.walk(start_path):
102        for f in filenames:
103            fp = os.path.join(dirpath, f)
104            # skip if it is symbolic link
105            if not os.path.islink(fp):
106                fp_allow_long_path = "\\\\?\\" + fp if util_os.is_windows() else fp
107                total_size += os.path.getsize(fp_allow_long_path)
108
109    return total_size
def get_total_dir_size_in_gigabytes(start_path):
112def get_total_dir_size_in_gigabytes(start_path):
113    """Calculate the total size of a directory in gigabytes.
114    Args:
115    start_path (str): The path of the directory to calculate size of.
116    Returns:
117    float: The total size of the directory in gigabytes."""
118    return get_total_dir_size_in_bytes(start_path) / TOTAL_BYTES_IN_GIGABYTE

Calculate the total size of a directory in gigabytes. Args: start_path (str): The path of the directory to calculate size of. Returns: float: The total size of the directory in gigabytes.

def get_unique_dirpath(path_to_dir):
121def get_unique_dirpath(path_to_dir):
122    """
123    Get a unique directory path similar to the given path.
124    """
125
126    def _ends_with_hyphen_number(path):
127        m = re.search(r"-\d+$", path)
128        return m is not None
129
130    # Avoid appending like x-01-02-03
131    if _ends_with_hyphen_number(path_to_dir):
132        path_to_dir = path_to_dir[:-3]
133
134    suffix = 2
135    path_to_dir_new = path_to_dir
136    while os.path.exists(path_to_dir_new):
137        path_to_dir_new = f"{path_to_dir}-{suffix:02}"
138        suffix += 1
139    return path_to_dir_new

Get a unique directory path similar to the given path.

def is_empty_directory(path_to_file):
142def is_empty_directory(path_to_file):
143    """Check if a directory is empty.
144    Args:
145    path_to_file (str): The path of the directory to check.
146    Returns:
147    bool: True if the directory is empty, False otherwise."""
148    if os.path.isfile(path_to_file):
149        return False
150    contents = os.listdir(path_to_file)
151    return len(contents) == 0

Check if a directory is empty. Args: path_to_file (str): The path of the directory to check. Returns: bool: True if the directory is empty, False otherwise.

def is_empty_directory_only_subdirectories(path_to_file):
154def is_empty_directory_only_subdirectories(path_to_file):
155    """Check if a directory is empty by inspecting subdirectories.
156    Args:
157    path_to_file (str): The path of the directory to check.
158    Returns:
159    bool: True if the directory is empty or only contains empty subdirectories, False otherwise."""
160    if os.path.isfile(path_to_file):
161        return False
162    contents = os.listdir(path_to_file)
163    for content in contents:
164        path_to_sub = os.path.join(path_to_file, content)
165        if os.path.isfile(path_to_sub):
166            return False
167        if not os.path.isfile(path_to_sub):
168            is_empty = is_empty_directory_only_subdirectories(path_to_sub)
169            if not is_empty:
170                return False
171    return True

Check if a directory is empty by inspecting subdirectories. Args: path_to_file (str): The path of the directory to check. Returns: bool: True if the directory is empty or only contains empty subdirectories, False otherwise.

def move_directory(path_to_dir, path_to_dir_renamed):
174def move_directory(path_to_dir, path_to_dir_renamed):
175    shutil.move(path_to_dir, path_to_dir_renamed)