def makedirs(self, path, mode=None, exist_ok=False): """ Make the directory `path`, but also make not yet existing intermediate directories, like `os.makedirs`. The value of `mode` is only accepted for compatibility with `os.makedirs` but otherwise ignored. If `exist_ok` is `False` (the default) and the leaf directory exists, raise a `PermanentError` with `errno` 17. """ path = ftputil.tool.as_str_path(path, encoding=self._encoding) path = self.path.abspath(path) directories = path.split(self.sep) old_dir = self.getcwd() try: # Try to build the directory chain from the "uppermost" to the # "lowermost" directory. for index in range(1, len(directories)): # Re-insert the separator which got lost by using `path.split`. next_directory = self.sep + self.path.join(*directories[: index + 1]) # If we have "virtual directories" (see #86), just listing the # parent directory won't tell us if a directory actually # exists. So try to change into the directory. try: self.chdir(next_directory) except ftputil.error.PermanentError: # Directory presumably doesn't exist. try: self.mkdir(next_directory) except ftputil.error.PermanentError: # Find out the cause of the error. Re-raise the # exception only if the directory didn't exist already, # else something went _really_ wrong, e. g. there's a # regular file with the name of the directory. if not self.path.isdir(next_directory): raise else: # Directory exists. If we are at the last directory # component and `exist_ok` is `False`, this is an error. if (index == len(directories) - 1) and (not exist_ok): # Before PEP 3151, if `exist_ok` is `False`, trying to # create an existing directory in the local file system # results in an `OSError` with `errno.EEXIST, so # emulate this also for FTP. ftp_os_error = ftputil.error.PermanentError( "path {!r} exists".format(path) ) ftp_os_error.errno = errno.EEXIST raise ftp_os_error finally: self.chdir(old_dir)
def makedirs(self, path, mode=None): """ Make the directory `path`, but also make not yet existing intermediate directories, like `os.makedirs`. The value of `mode` is only accepted for compatibility with `os.makedirs` but otherwise ignored. """ path = ftputil.tool.as_unicode(path) path = self.path.abspath(path) directories = path.split(self.sep) old_dir = self.getcwd() try: # Try to build the directory chain from the "uppermost" to # the "lowermost" directory. for index in range(1, len(directories)): # Re-insert the separator which got lost by using # `path.split`. next_directory = (self.sep + self.path.join(*directories[:index+1])) # If we have "virtual directories" (see #86), just # listing the parent directory won't tell us if a # directory actually exists. So try to change into the # directory. try: self.chdir(next_directory) except ftputil.error.PermanentError: try: self.mkdir(next_directory) except ftputil.error.PermanentError: # Find out the cause of the error. Re-raise # the exception only if the directory didn't # exist already, else something went _really_ # wrong, e. g. there's a regular file with the # name of the directory. if not self.path.isdir(next_directory): raise finally: self.chdir(old_dir)
def makedirs(self, path, mode=None): """ Make the directory `path`, but also make not yet existing intermediate directories, like `os.makedirs`. The value of `mode` is only accepted for compatibility with `os.makedirs` but otherwise ignored. """ path = ftputil.tool.as_unicode(path) path = self.path.abspath(path) directories = path.split(self.sep) # Try to build the directory chain from the "uppermost" to # the "lowermost" directory. for index in range(1, len(directories)): # Re-insert the separator which got lost by using `path.split`. next_directory = self.sep + self.path.join(*directories[:index+1]) try: self.mkdir(next_directory) except ftputil.error.PermanentError: # Find out the cause of the error. Re-raise the # exception only if the directory didn't exist already, # else something went _really_ wrong, e. g. there's a # regular file with the name of the directory. if not self.path.isdir(next_directory): raise