Create a directory. Args: path (str): The directory to create. create_parents (Optional[bool]): Whether to create parent directories if they don't exist. Defaults to True.
(self, path: str, create_parents: Optional[bool] = True)
| 106 | self.service = srv.files() |
| 107 | |
| 108 | def mkdir(self, path: str, create_parents: Optional[bool] = True) -> None: |
| 109 | """Create a directory. |
| 110 | |
| 111 | Args: |
| 112 | path (str): The directory to create. |
| 113 | create_parents (Optional[bool]): |
| 114 | Whether to create parent directories if they don't exist. |
| 115 | Defaults to True. |
| 116 | """ |
| 117 | # if there are more than two components, create parents (first component is root id, second - path being created) |
| 118 | if create_parents and len(path.split("/")) > 2: |
| 119 | self.makedirs(self._parent(path), exist_ok=True) |
| 120 | parent_id = self.path_to_file_id(self._parent(path)) |
| 121 | name = path.rstrip("/").rsplit("/", 1)[-1] |
| 122 | meta = { |
| 123 | "name": name, |
| 124 | "mimeType": DIR_MIME_TYPE, |
| 125 | "parents": [parent_id], |
| 126 | } |
| 127 | file = self.service.create(body=meta, supportsAllDrives=True).execute() |
| 128 | # cache the new dir |
| 129 | FILE_ID_CACHE[(parent_id, name)] = file["id"] |
| 130 | try: |
| 131 | self.invalidate_cache(parent_id) |
| 132 | except Exception: |
| 133 | # allow for parent to not exist as valid path |
| 134 | pass |
| 135 | |
| 136 | def makedirs(self, path: str, exist_ok: Optional[bool] = True) -> None: |
| 137 | """Create a directory and all its parent components. |