pyarrow.fs.PyFileSystem#
- class pyarrow.fs.PyFileSystem(handler)#
Bases:
FileSystem行为在 Python 中实现的 FileSystem。
- 参数:
- handler
FileSystemHandler 实现自定义文件系统行为的处理程序对象。
- handler
示例
为 GitHub 创建一个基于 fsspec 的文件系统对象
>>> from fsspec.implementations import github >>> gfs = github.GithubFileSystem('apache', 'arrow')
获取一个 PyArrow FileSystem 对象
>>> from pyarrow.fs import PyFileSystem, FSSpecHandler >>> pa_fs = PyFileSystem(FSSpecHandler(gfs))
使用
FileSystem()功能get_file_info()>>> pa_fs.get_file_info('README.md') <FileInfo for 'README.md': type=FileType.File, size=...>
- __init__(*args, **kwargs)#
方法
__init__(*args, **kwargs)copy_file(self, src, dest)复制文件。
create_dir(self, path, *, bool recursive=True)创建目录及子目录。
delete_dir(self, path)递归删除目录及其内容。
delete_dir_contents(self, path, *, ...)递归删除目录内容。
delete_file(self, path)删除文件。
equals(self, FileSystem other)from_uri(uri)从 URI 或路径创建新的 FileSystem。
get_file_info(self, paths_or_selector)获取给定文件的信息。
move(self, src, dest)移动/重命名文件或目录。
normalize_path(self, path)规范化文件系统路径。
open_append_stream(self, path[, ...])打开用于追加的输出流。
open_input_file(self, path)打开用于随机访问读取的输入文件。
open_input_stream(self, path[, compression, ...])打开用于顺序读取的输入流。
open_output_stream(self, path[, ...])打开用于顺序写入的输出流。
属性
- copy_file(self, src, dest)#
复制文件。
如果目标已存在且是目录,则返回错误。否则,它将被替换。
示例
>>> local.copy_file(path, ... local_path + '/pyarrow-fs-example_copy.dat')
检查文件信息
>>> local.get_file_info(local_path + '/pyarrow-fs-example_copy.dat') <FileInfo for '/.../pyarrow-fs-example_copy.dat': type=FileType.File, size=4> >>> local.get_file_info(path) <FileInfo for '/.../pyarrow-fs-example.dat': type=FileType.File, size=4>
- create_dir(self, path, *, bool recursive=True)#
创建目录及子目录。
如果目录已存在,此函数成功。
- delete_dir_contents(self, path, *, bool accept_root_dir=False, bool missing_dir_ok=False)#
递归删除目录的内容。
类似于 delete_dir,但不会删除目录本身。
- equals(self, FileSystem other)#
- 参数:
- 返回:
- static from_uri(uri)#
从 URI 或路径创建新的 FileSystem。
可识别的 URI 方案包括“file”、“mock”、“s3fs”、“gs”、“gcs”、“hdfs”和“viewfs”。此外,参数可以是 pathlib.Path 对象,或描述绝对本地路径的字符串。
- 参数:
- uri
str 基于 URI 的路径,例如:file:///some/local/path。
- uri
- 返回:
tupleof (FileSystem,strpath)带有 (filesystem, path) 元组,其中 path 是 FileSystem 实例内的抽象路径。
示例
从 URI 创建新的 FileSystem 子类
>>> uri = f'file:///{local_path}/pyarrow-fs-example.dat' >>> local_new, path_new = fs.FileSystem.from_uri(uri) >>> local_new <pyarrow._fs.LocalFileSystem object at ... >>> path_new '/.../pyarrow-fs-example.dat'
或者从 s3 存储桶创建
>>> fs.FileSystem.from_uri("s3://usgs-landsat/collection02/") (<pyarrow._s3fs.S3FileSystem object at ...>, 'usgs-landsat/collection02')
或者从 fsspec+ URI 创建
>>> fs.FileSystem.from_uri("fsspec+memory:///path/to/file") (<pyarrow._fs.PyFileSystem object at ...>, '/path/to/file')
- get_file_info(self, paths_or_selector)#
获取给定文件的信息。
任何符号链接都会自动递归解引用。不存在或不可访问的文件将返回一个 FileStat 对象,其 FileType 值为 NotFound。异常表示真正异常的情况(低级 I/O 错误等)。
- 参数:
- paths_or_selector
FileSelector, path-like orlistof path-likes 可以是选择器对象、类路径对象或类路径对象列表。选择器的基本目录不会作为结果的一部分返回,即使它存在。如果它不存在,请使用 allow_not_found。
- paths_or_selector
- 返回:
示例
>>> local <pyarrow._fs.LocalFileSystem object at ...> >>> local.get_file_info(f"/{local_path}/pyarrow-fs-example.dat") <FileInfo for '/.../pyarrow-fs-example.dat': type=FileType.File, size=4>
- handler#
文件系统底层处理程序。
- 返回:
- handler
FileSystemHandler
- handler
- move(self, src, dest)#
移动/重命名文件或目录。
如果目标存在: - 如果它是一个非空目录,则返回错误 - 否则,如果它与源具有相同类型,则替换它 - 否则,行为不确定(取决于实现)。
示例
创建一个包含文件的新文件夹
>>> local.create_dir('/tmp/other_dir') >>> local.copy_file(path,'/tmp/move_example.dat')
移动文件
>>> local.move('/tmp/move_example.dat', ... '/tmp/other_dir/move_example_2.dat')
检查文件信息
>>> local.get_file_info('/tmp/other_dir/move_example_2.dat') <FileInfo for '/tmp/other_dir/move_example_2.dat': type=FileType.File, size=4> >>> local.get_file_info('/tmp/move_example.dat') <FileInfo for '/tmp/move_example.dat': type=FileType.NotFound>
删除文件夹:>>> local.delete_dir(‘/tmp/other_dir’)
- open_append_stream(self, path, compression='detect', buffer_size=None, metadata=None)#
打开用于追加的输出流。
如果目标不存在,则创建新的空文件。
注意
某些文件系统实现不支持对现有文件进行高效追加,在这种情况下,此方法将引发 NotImplementedError。考虑写入多个文件(例如使用数据集层)而不是追加。
- 参数:
- path
str 用于写入的源。
- compression
str可选,默认 ‘detect’ 用于即时压缩的压缩算法。如果为 “detect” 且源是文件路径,则会根据文件扩展名选择压缩方式。如果为 None,则不应用压缩。否则,必须提供一个知名的算法名称(例如 “gzip”)。
- buffer_size
intoptional, defaultNone 如果为 None 或 0,则不进行缓冲。否则为临时写入缓冲区的大小。
- metadata
dictoptional, defaultNone 如果不是 None,则为字符串键到字符串值的映射。某些文件系统支持在文件旁存储元数据(例如“Content-Type”)。不支持的元数据键将被忽略。
- path
- 返回:
- stream
NativeFile
- stream
示例
将新数据追加到包含非空文件的 FileSystem 子类
>>> with local.open_append_stream(path) as f: ... f.write(b'+newly added') 12
将内容打印到文件
>>> with local.open_input_file(path) as f: ... print(f.readall()) b'data+newly added'
- open_input_file(self, path)#
打开用于随机访问读取的输入文件。
- 参数:
- path
str 用于读取的源。
- path
- 返回:
- stream
NativeFile
- stream
示例
使用 open_input_file() 打印文件中的数据
>>> with local.open_input_file(path) as f: ... print(f.readall()) b'data'
- open_input_stream(self, path, compression='detect', buffer_size=None)#
打开用于顺序读取的输入流。
- 参数:
- 返回:
- stream
NativeFile
- stream
示例
使用 open_input_stream() 打印文件中的数据
>>> with local.open_input_stream(path) as f: ... print(f.readall()) b'data'
- open_output_stream(self, path, compression='detect', buffer_size=None, metadata=None)#
打开用于顺序写入的输出流。
如果目标已存在,则截断现有数据。
- 参数:
- path
str 用于写入的源。
- compression
str可选,默认 ‘detect’ 用于即时压缩的压缩算法。如果为 “detect” 且源是文件路径,则会根据文件扩展名选择压缩方式。如果为 None,则不应用压缩。否则,必须提供一个知名的算法名称(例如 “gzip”)。
- buffer_size
intoptional, defaultNone 如果为 None 或 0,则不进行缓冲。否则为临时写入缓冲区的大小。
- metadata
dictoptional, defaultNone 如果不是 None,则为字符串键到字符串值的映射。某些文件系统支持在文件旁存储元数据(例如“Content-Type”)。不支持的元数据键将被忽略。
- path
- 返回:
- stream
NativeFile
- stream
示例
>>> local = fs.LocalFileSystem() >>> with local.open_output_stream(path) as stream: ... stream.write(b'data') 4
- type_name#
文件系统的类型名称。