PyFilesystem是否支持多种模式组合使用?
PyFilesystem: Support for Combined Filesystem Modes
Absolutely! PyFilesystem fully supports combining multiple filesystem modes exactly as you described—using a URL like ftp+tar://user:password@host:port/path/file.tar.gz to access a tar file hosted on an FTP server works perfectly.
How it works
PyFilesystem uses a "nested filesystem" approach, where the + in the URL tells the library to chain together different filesystem drivers. Here’s what happens under the hood:
- It first uses the FTP driver to establish a connection to the remote server and locate the target tar.gz file.
- Then it mounts that tar file as a virtual filesystem using the Tar driver, letting you interact with the contents of the tar directly without downloading the entire file first.
Two ways to use this combination
1. Directly via FS URL (simplest approach)
You can open the combined filesystem in one line using fs.open_fs():
from fs import open_fs with open_fs("ftp+tar://user:password@host:port/path/file.tar.gz") as combined_fs: # List files inside the tar archive print(combined_fs.listdir("/")) # Read a file from the tar with combined_fs.opentext("document.txt") as f: print(f.read())
2. Manual nesting (for more control)
If you need to customize settings (like FTP timeouts, tar compression options), you can manually nest the filesystems:
from fs.ftpfs import FTPFS from fs.tarfs import TarFS # Connect to the FTP server with custom settings with FTPFS( host="your-host", user="your-username", passwd="your-password", port=21, timeout=30 ) as ftp_fs: # Open the remote tar.gz file as a binary stream with ftp_fs.openbin("/path/to/file.tar.gz") as tar_stream: # Mount the stream as a Tar filesystem with TarFS(tar_stream) as tar_fs: # Interact with the tar contents tar_fs.copy("/report.pdf", "./local-report.pdf")
Important Notes
- Make sure you have the required dependencies installed: Run
pip install fs[ftp,tar]to get both the FTP and Tar filesystem drivers. - While not all driver combinations work seamlessly, FTP + Tar is officially supported because TarFS can operate on a binary stream (which FTPFS provides for remote files).
内容的提问来源于stack exchange,提问作者Anton Daneyko
相关产品推荐
相关产品推荐

