|
| 1 | +import os |
| 2 | + |
| 3 | +from typing import Union |
| 4 | + |
| 5 | +def ensure_file( |
| 6 | + path: Union[str, bytes], *, |
| 7 | + folder: bool = False, |
| 8 | + default_value: Union[str, bytes] = b"", |
| 9 | + auto_correct_type: bool = True, |
| 10 | + throw_error: bool = False, |
| 11 | + throw_check_type: bool = True |
| 12 | +) -> bool: |
| 13 | + '''Creates a file or folder if it doesn't exist. |
| 14 | +
|
| 15 | + path: string - The file/folder path |
| 16 | + folder: boolean - Whether or not to create a folder instead of a file. |
| 17 | + default_value: string | bytes - The default value to put into a created file (assuming folder is false). |
| 18 | + auto_correct_type: boolean - Whether or not to delete the file and recreate it if it is a folder instead of a file or vice versa. |
| 19 | + throw_error: boolean - Throws an error if the file doesn't exist, instead of creating a new one. |
| 20 | + throw_check_type: boolean - Requires throw_error to be true. This also throws an error if a folder is meant to be a file or vice versa. |
| 21 | +
|
| 22 | + Returns true if a file was created or the file type was swapped and false if the file wasn't created.''' |
| 23 | + |
| 24 | + # Type checking |
| 25 | + if isinstance(path, bytes): |
| 26 | + path: str = bytes.decode(path) |
| 27 | + if not isinstance(path, str): |
| 28 | + raise TypeError(f"'path' should be str or bytes, not {type(path)}") |
| 29 | + if not isinstance(folder, bool): |
| 30 | + folder: bool = bool(folder) |
| 31 | + if isinstance(default_value, str): |
| 32 | + default_value: bytes = str.encode(default_value) |
| 33 | + if not isinstance(default_value, bytes): |
| 34 | + default_value: bytes = str.encode(str(default_value)) |
| 35 | + if not isinstance(auto_correct_type, bool): |
| 36 | + auto_correct_type: bool = bool(auto_correct_type) |
| 37 | + if not isinstance(throw_error, bool): |
| 38 | + throw_error: bool = bool(throw_error) |
| 39 | + if not isinstance(throw_check_type, bool): |
| 40 | + throw_check_type: bool = bool(throw_check_type) |
| 41 | + |
| 42 | + # Expand the user part of a path |
| 43 | + if path and path[0] == "~": |
| 44 | + path: str = os.path.expanduser(path) |
| 45 | + |
| 46 | + # Throws the errors if needed |
| 47 | + if throw_error: |
| 48 | + if not os.path.exists(path): |
| 49 | + raise FileNotFoundError(f"The {'folder' if folder else 'file'} at {path} doesn't exist.") |
| 50 | + |
| 51 | + if throw_check_type and ((os.path.isdir(path) and not folder) or (not os.path.isdir(path) and folder)): |
| 52 | + raise FileExistsError(f"The {'file' if folder else 'folder'} at {path} should be a {'folder' if folder else 'file'}.") |
| 53 | + |
| 54 | + return False |
| 55 | + |
| 56 | + if os.path.exists(path): |
| 57 | + # File: exists; Type: Folder; Should be: File |
| 58 | + if auto_correct_type and os.path.isdir(path) and not folder: |
| 59 | + os.rmdir(path) |
| 60 | + f = open(path, "wb") |
| 61 | + f.write(default_value) |
| 62 | + f.close() |
| 63 | + return True |
| 64 | + |
| 65 | + # File: exists; Type: File; Should be: Fplder |
| 66 | + if auto_correct_type and not os.path.isdir(path) and folder: |
| 67 | + os.remove(path) |
| 68 | + os.mkdir(path) |
| 69 | + return True |
| 70 | + |
| 71 | + # File: exists; Type: File | Folder; Should be: File | Folder |
| 72 | + return False |
| 73 | + |
| 74 | + if folder: |
| 75 | + # File: doesn't exist; Should be: Folder |
| 76 | + os.mkdir(path) |
| 77 | + else: |
| 78 | + # File: doesn't exist; Should be: File |
| 79 | + f = open(path, "wb") |
| 80 | + f.write(default_value) |
| 81 | + f.close() |
| 82 | + |
| 83 | + return True |
0 commit comments