diff --git a/fsspec/implementations/sftp.py b/fsspec/implementations/sftp.py index 47a14f1a2..052b8261f 100644 --- a/fsspec/implementations/sftp.py +++ b/fsspec/implementations/sftp.py @@ -80,12 +80,13 @@ def makedirs(self, path, exist_ok=False, mode=511): raise FileExistsError(f"File exists: {path}") parts = path.split("/") - path = "" + new_path = "/" if path[:1] == "/" else "" for part in parts: - path += f"/{part}" - if not self.exists(path): - self.ftp.mkdir(path, mode) + if part: + new_path = f"{new_path}/{part}" if new_path else part + if not self.exists(new_path): + self.ftp.mkdir(new_path, mode) def rmdir(self, path): logger.debug("Removing folder %s", path) diff --git a/fsspec/implementations/tests/test_sftp.py b/fsspec/implementations/tests/test_sftp.py index a38104953..47218ab0d 100644 --- a/fsspec/implementations/tests/test_sftp.py +++ b/fsspec/implementations/tests/test_sftp.py @@ -200,27 +200,32 @@ def test_transaction(ssh, root_path): f.rm(root_path, recursive=True) -def test_mkdir_create_parent(ssh): +@pytest.mark.parametrize("path", ["/a/b/c", "a/b/c"]) +def test_mkdir_create_parent(ssh, path): f = fsspec.get_filesystem_class("sftp")(**ssh) with pytest.raises(FileNotFoundError): - f.mkdir("/a/b/c") + f.mkdir(path) - f.mkdir("/a/b/c", create_parents=True) - assert f.exists("/a/b/c") + f.mkdir(path, create_parents=True) + assert f.exists(path) - with pytest.raises(FileExistsError, match="/a/b/c"): - f.mkdir("/a/b/c") + with pytest.raises(FileExistsError, match=path): + f.mkdir(path) - f.rm("/a/b/c", recursive=True) + f.rm(path, recursive=True) + assert not f.exists(path) -def test_makedirs_exist_ok(ssh): +@pytest.mark.parametrize("path", ["/a/b/c", "a/b/c"]) +def test_makedirs_exist_ok(ssh, path): f = fsspec.get_filesystem_class("sftp")(**ssh) - f.makedirs("/a/b/c") + f.makedirs(path) - with pytest.raises(FileExistsError, match="/a/b/c"): - f.makedirs("/a/b/c", exist_ok=False) + with pytest.raises(FileExistsError, match=path): + f.makedirs(path, exist_ok=False) - f.makedirs("/a/b/c", exist_ok=True) + f.makedirs(path, exist_ok=True) + f.rm(path, recursive=True) + assert not f.exists(path)