Conversation
| existed = await self._sftp.exists(str(path)) | ||
| if existed and not exist_ok: | ||
| raise FileExistsError(f"{path} already exists") | ||
| await self._sftp.mkdir(str(path)) |
There was a problem hiding this comment.
🔴 mkdir with exist_ok=True and parents=False fails when directory exists
When calling mkdir(path, parents=False, exist_ok=True) on an existing directory, the method will fail instead of succeeding silently.
Click to expand
Root Cause
The logic at lines 404-407 checks if the directory exists and raises FileExistsError only when exist_ok=False. However, when exist_ok=True and the directory exists, the code still proceeds to call await self._sftp.mkdir(str(path)) which will fail because the directory already exists.
existed = await self._sftp.exists(str(path))
if existed and not exist_ok:
raise FileExistsError(f"{path} already exists")
await self._sftp.mkdir(str(path)) # Called even when existed=True and exist_ok=True!Expected Behavior
When exist_ok=True and the directory already exists, the method should return successfully without attempting to create the directory.
Actual Behavior
The method attempts to create the directory even when it exists, causing an SFTP error.
Fix
The code should skip the mkdir call when the directory already exists:
if existed:
if not exist_ok:
raise FileExistsError(f"{path} already exists")
else:
await self._sftp.mkdir(str(path))Note: This is a pre-existing bug that was present before this PR.
Recommendation: Change the logic to skip mkdir when directory exists: if existed: if not exist_ok: raise FileExistsError(...) else: await self._sftp.mkdir(str(path))
Was this helpful? React with 👍 or 👎 to provide feedback.
…ge for logging exceptions
Uh oh!
There was an error while loading. Please reload this page.