Skip to content

Make recursive get, put and copy work for folders - #424

Merged
shcheklein merged 3 commits into
mainfrom
recursive-transfers
Dec 1, 2026
Merged

shcheklein merged 3 commits into
mainfrom
recursive-transfers

Conversation

@shcheklein

@shcheklein shcheklein commented Dec 1, 2026 •

Copy link
Copy Markdown
Member

fs.get, fs.put and fs.copy with recursive=True fail on the first folder they meet:

fs.put("src", "root/up", recursive=True)
# IsADirectoryError: 'src' (PermissionError on Windows)
fs.get("root/data", "out", recursive=True)
# ApiRequestError: 403 "Export only supports Docs Editors files."
fs.copy("root/data", "root/copy", recursive=True)
# ApiRequestError: 403 "This file cannot be copied by the user."

fsspec calls get_file, put_file and cp_file for every entry of the tree, folders included, and expects each one to create the folder. The three overrides in GDriveFileSystem treated a folder as a file.

Changes

  • put_file creates the Drive folder when the local path is a directory.
  • get_file creates the local directory and cp_file creates the destination folder when the source is a Drive folder. Both read this from the path lookup they already make, so a file transfer makes the same requests as before.
  • get_file creates the missing local parent folders, as fsspec's own get_file does. Without this, fs.get(source, target, recursive=True, maxdepth=2) and a glob download into a new directory failed with FileNotFoundError.
  • A folder can be copied onto the filesystem root, for example fs.copy("root/source/", "root", recursive=True). Copying a file there still requires a filename, now after one lookup of the source.
  • When the filesystem root is a bare ID, the first get_file or cp_file of the root itself asks Drive once whether that ID is a folder. The answer is cached. A trashed ID raises FileNotFoundError, as info() already does.

Validation

  • The first commit has only the live tests. On Drive, the three recursive tests failed in each of the 14 matrix jobs that finished, and every other test passed (run).
  • Offline tests cover each folder branch, the bare-ID cases and the request counts. Four of them fail on the second commit and pass on the third: the trashed IDs, the copy onto the root and the local parent folders.
  • With the fix: CI checks.
  • Requests against a simulated Drive, for 1,000 files in 10 folders: get_file and cp_file still make 2 requests per file. Recursive get makes 2,003, the same as calling get_file for each path that find() returns.

🤖 Generated with Claude Code

shcheklein and others added 2 commits November 30, 2026 18:55
Each test moves a small tree with a nested file and an empty folder. They
fail today: get_file, put_file and cp_file treat a folder as a file.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
fsspec calls these for every entry of a recursive get, put or copy and
expects a folder entry to create the folder. A file transfer makes the
same requests as before: get_file and cp_file read the folder flag from
the path lookup they already make.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Copying a directory to root still raises a filename-required error because destination validation runs before source classification; folder handling and the corresponding test need updating.

1 open issue
What changed in this PR

This PR makes GDriveFileSystem handle directory entries during fsspec’s recursive uploads, downloads, and copies.

Changes:

  • Create destination folders instead of attempting file transfers for directory entries.
  • Reuse path lookup information to identify folders, with cached metadata for bare-ID roots.
  • Add integration tests for nested files, empty folders, and downloads by Drive ID.
File Description
pydrive2/​test/​test_fs.py Adds recursive transfer and bare-ID download tests.
pydrive2/​fs/​spec.py Adds folder detection and directory handling to transfer methods.

Review effort: Balanced

Comment thread pydrive2/fs/spec.py Resolved

@shcheklein shcheklein left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The core approach (reading the folder flag from _lookup_path so file transfers don't incur extra Drive requests) fits DirectoryCache nicely. Left a few inline comments covering three edge cases (cp_file targeting the root, trashed bare root IDs in _lookup_item, and missing local parent dirs in get_file) plus offline unit test coverage.

Comment thread pydrive2/fs/spec.py Resolved
Comment thread pydrive2/fs/spec.py Resolved
Comment thread pydrive2/fs/spec.py Resolved
Comment thread pydrive2/test/test_fs.py Resolved
- A trashed bare ID raises FileNotFoundError in get_file and cp_file.
- cp_file accepts a folder copied onto the filesystem root.
- get_file creates the missing local parent folders.
- Offline tests cover the folder branches and their request counts.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@shcheklein
shcheklein marked this pull request as ready for review December 1, 2026 15:15
@shcheklein
shcheklein merged commit 356369d into main Dec 1, 2026
24 checks passed
@shcheklein
shcheklein deleted the recursive-transfers branch December 1, 2026 15:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants