fs.opener¶
从URL打开文件系统
-
class
fs.opener.Opener¶ The opener base class.
An opener is responsible for opening a filesystems from one or more protocols. A list of supported protocols is supplied in a class attribute called protocols.
Openers should be registered with a
Registryobject, which picks an appropriate opener object for a given FS URL.-
open_fs(fs_url, parse_result, writeable, create, cwd)¶ Open a filesystem object from a FS URL.
参数: - fs_url (str) – A filesystem URL
- parse_result (
ParseResult) – A parsed filesystem URL. - writeable (bool) – True if the filesystem must be writeable.
- create (bool) – True if the filesystem should be created if it does not exist.
- cwd (str) – The current working directory (generally only relevant for OS filesystems).
返回: FSobject
-
-
exception
fs.opener.OpenerError¶ Base class for opener related errors.
-
exception
fs.opener.ParseError¶ Raised when attempting to parse an invalid FS URL.
-
class
fs.opener.ParseResult(protocol, username, password, resource, path)¶ -
password¶ Alias for field number 2
-
path¶ Alias for field number 4
-
protocol¶ Alias for field number 0
-
resource¶ Alias for field number 3
-
username¶ Alias for field number 1
-
-
class
fs.opener.Registry(default_opener='osfs')¶ A registry for Opener instances.
-
install(opener)¶ Install an opener.
参数: opener – An Openerinstance, or a callable that returns an opener instance.May be used as a class decorator. For example:
registry = Registry() @registry.install class ArchiveOpener(Opener): protocols = ['zip', 'tar']
-
open(fs_url, writeable=True, create=False, cwd='.', default_protocol='osfs')¶ Open a filesystem from a FS URL. Returns a tuple of a filesystem object and a path. If there is no path in the FS URL, the path value will be
None.参数: - fs_url (str) – A filesystem URL
- writeable (bool) – True if the filesystem must be writeable.
- create (bool) – True if the filesystem should be created if it does not exist.
- cwd (str or None) – The current working directory.
返回类型: Tuple of
(<filesystem>, <path from url>)
-
open_fs(fs_url, writeable=True, create=False, cwd='.', default_protocol='osfs')¶ Open a filesystem object from a FS URL (ignoring the path component).
参数: - fs_url (str) – A filesystem URL
- parse_result (
ParseResult) – A parsed filesystem URL. - writeable (bool) – True if the filesystem must be writeable.
- create (bool) – True if the filesystem should be created if it does not exist.
- cwd (str) – The current working directory (generally only relevant for OS filesystems).
- default_protocol (str) – The protocol to use if one is not
supplied in the FS URL (defaults to
"osfs").
返回: FSobject
-
-
exception
fs.opener.Unsupported¶ May be raised by opener if the opener fails to open a FS.
-
fs.opener.manage_fs(fs_url, create=False, writeable=True, cwd='.')¶ A context manager opens / closes a filesystem.
参数: Sometimes it is convenient to be able to pass either a FS object or an FS URL to a function. This context manager handles the required logic for that.
Here’s an example:
def print_ls(list_fs): """List a directory.""" with manage_fs(list_fs) as fs: print(" ".join(fs.listdir()))
This function may be used in two ways. You may either pass either a
str, as follows:print_list('zip://projects.zip')
Or, an FS instance:
from fs.osfs import OSFS projects_fs = OSFS('~/') print_list(projects_fs)
-
fs.opener.parse(fs_url)¶ Parse a Filesystem URL and return a
ParseResult, or raiseParseError(subclass of ValueError) if the FS URL is not value.参数: fs_url (str) – A filesystem URL 返回类型: ParseResult