def rename_folder(directory, name, make_unique=False): """ Renames given with a new name :param directory: str, full path to the diretory we want to rename :param name: str, new name of the folder we want to rename :param make_unique: bool, Whether to add a number to the folder name to make it unique :return: str, path of the renamed folder """ from tpDcc.libs.python import path base_name = path.get_basename(directory=directory) if base_name == name: return parent_path = path.get_dirname(directory=directory) rename_path = path.join_path(parent_path, name) if make_unique: rename_path = path.unique_path_name(directory=rename_path) if path.is_dir(rename_path) or path.is_file(rename_path): return False try: os.chmod(directory, 0o777) message = 'rename: {0} >> {1}'.format(directory, rename_path) LOGGER.info(message) os.rename(directory, rename_path) except Exception: LOGGER.error('{}'.format(traceback.format_exc())) return False return rename_path
def create_file(filename, directory=None, make_unique=False): """ Creates a file :param filename: str, name of the new file :param directory: str, directory of the new file :param make_unique: bool, whether to make the name unique or not :return: variant, str || bool, filename with path or False if create file failed """ from tpDcc.libs.python import name, path, osplatform if directory is None: directory = path.get_dirname(filename) filename = path.get_basename(filename) filename = name.clean_file_string(filename) full_path = path.join_path(directory, filename) if make_unique: full_path = path.unique_path_name(full_path) open_file = None try: open_file = open(full_path, 'a') open_file.close() except Exception: if open_file: open_file.close() return False osplatform.get_permission(full_path) return full_path
def create_folder(name, directory=None, make_unique=False): """ Creates a new folder on the given path and with the given name :param name: str, name of the new directory :param directory: str, path to the new directory :param make_unique: bool, Whether to pad the name with a number to make it unique if the folder is not unique :return: variant, str || bool, folder name with path or False if the folder creation failed """ from tpDcc.libs.python import path, osplatform full_path = False if directory is None: full_path = name if not name: full_path = directory if name and directory: full_path = path.join_path(directory, name) if make_unique: full_path = path.unique_path_name(directory=full_path) if not full_path: return False if path.is_dir(full_path): return full_path try: os.makedirs(full_path) except Exception: return False osplatform.get_permission(full_path) return full_path
def rename_folder(directory, name, make_unique=False): """ Renames given with a new name :param directory: str, full path to the directory we want to rename :param name: str, new name of the folder we want to rename :param make_unique: bool, Whether to add a number to the folder name to make it unique :return: str, path of the renamed folder """ from tpDcc.libs.python import path, osplatform base_name = path.get_basename(directory=directory) if base_name == name: return parent_path = path.get_dirname(directory=directory) rename_path = path.join_path(parent_path, name) if make_unique: rename_path = path.unique_path_name(directory=rename_path) if path.exists(rename_path): return False try: osplatform.get_permission(directory) message = 'rename: {0} >> {1}'.format(directory, rename_path) logger.info(message) os.rename(directory, rename_path) except Exception: time.sleep(0.1) try: os.rename(directory, rename_path) except Exception: logger.error('{}'.format(traceback.format_exc())) return False return rename_path
def _increment_version_file_name(self): version_path = path.join_path(self._version_folder, self._version_name + '.1') return path.unique_path_name(version_path)
def _increment_version_file_name(self): from tpDcc.libs.python import path version_path = path.join_path(self._version_folder, self._version_name + '.1') return path.unique_path_name(directory=version_path)