snowflake.snowpark.files.SnowflakeFile

class snowflake.snowpark.files.SnowflakeFile(file_location: str, mode: str = 'r', is_owner_file: bool = False, *, require_scoped_url: bool = True, from_result_api: bool = False)[source] (https://github.com/snowflakedb/snowpark-python/blob/v1.35.0/src/snowflake/snowpark/files.py#L45-L506)

Bases: RawIOBase

SnowflakeFile provides an interface to operate on files as Python IOBase-like objects in UDFs and stored procedures. SnowflakeFile supports most operations supported by Python IOBase objects. A SnowflakeFile object can be used as a Python IOBase object.

The constructor of this class is not supposed to be called directly. Call open() to create a read-only SnowflakeFile object, and call open_new_result() to create a write-only SnowflakeFile object.

This class is used to read and write files in UDFs and stored procedures. On Snowflake, it is used to read and write stage files. It also supports Python IOBase and BufferedBase methods such as read(), write(), close().

To read from a staged file, use the following API:

Example::
>>> from snowflake.snowpark.files import SnowflakeFile
>>> from snowflake.snowpark.functions import udf
>>> @udf
... def read_file(url: str) -> str:
...     file = SnowflakeFile.open(url, "r")
...     return file.read()
Copy

To write to a staged file first write to a result file via the following example. The result file will return as a scoped URL which can be copied to a permanent stage with copy files or read directly via another call to SnowflakeFile.read() in another UDF invocation. See writing files from Snowpark Python UDFs for more details.

Example::
>>> from snowflake.snowpark.files import SnowflakeFile
>>> from snowflake.snowpark.functions import udf
>>> @udf
... def write_file(content: str) -> str:
...     file = SnowflakeFile.open_new_result("w")
...     file.write(content)
...     return file # file must be returned to be accessible
Copy

These examples are using the client, but this same pattern can be used inside SQL-defined UDFs.

We provide a local implementation of SnowflakeFile to aid in local testing. This currently supports using read APIs on relative paths, mocked stages (sessions in local testing mode that aren’t connected to a real stage), and Snowflake stages.

Note

  1. All of the implementation in this file is for local testing purposes.

2. There may be slight implementation differences between local testing and Snowflake execution environments, which we call out below when we can. If any issues or these differences block your testing workflow, please file a bug report at https://github.com/snowflakedb/snowpark-python/issues (https://github.com/snowflakedb/snowpark-python/issues).

  1. UDF implementation is dependent on the Snowflake release. Therefore, this documentation may not always be up to date.

Methods

close()

See https://docs.python.org/3/library/io.html#io.IOBase.close (https://docs.python.org/3/library/io.html#io.IOBase.close)

detach()

Not yet supported in UDF and Stored Procedures.

fileno()

Getting a file descriptor number is not supported in Snowflake.

flush()

Fail if the stream is closed.

isatty()

Returns False, file streams in stored procedures and UDFs are never interactive in Snowflake.

open(file_location[, mode, is_owner_file, ...])

Used to create a SnowflakeFile which can only be used for read-based IO operations on the file.

open_new_result([mode])

Used to create a SnowflakeFile which can only be used for write-based IO operations.

read([size])

From https://docs.python.org/3/library/io.html#io.RawIOBase.read (https://docs.python.org/3/library/io.html#io.RawIOBase.read)

read1([size])

From https://docs.python.org/3/library/io.html#io.BufferedIOBase.read1 (https://docs.python.org/3/library/io.html#io.BufferedIOBase.read1)

readable()

From https://docs.python.org/3/library/io.html#io.IOBase.readable (https://docs.python.org/3/library/io.html#io.IOBase.readable)

readall()

From https://docs.python.org/3/library/io.html#io.RawIOBase.readall (https://docs.python.org/3/library/io.html#io.RawIOBase.readall)

readinto(b)

From https://docs.python.org/3/library/io.html#io.RawIOBase.readinto (https://docs.python.org/3/library/io.html#io.RawIOBase.readinto)

readinto1(b)

From https://docs.python.org/3/library/io.html#io.BufferedIOBase.readinto1 (https://docs.python.org/3/library/io.html#io.BufferedIOBase.readinto1)

readline([size])

From https://docs.python.org/3/library/io.html#io.IOBase.readline (https://docs.python.org/3/library/io.html#io.IOBase.readline)

readlines([hint])

From https://docs.python.org/3/library/io.html#io.IOBase.readlines (https://docs.python.org/3/library/io.html#io.IOBase.readlines)

seek(offset[, whence])

See https://docs.python.org/3/library/io.html#io.IOBase.seek (https://docs.python.org/3/library/io.html#io.IOBase.seek)

seekable()

See https://docs.python.org/3/library/io.html#io.IOBase.seekable (https://docs.python.org/3/library/io.html#io.IOBase.seekable)

tell()

See https://docs.python.org/3/library/io.html#io.IOBase.tell (https://docs.python.org/3/library/io.html#io.IOBase.tell)

truncate([size])

Not yet supported in UDF and Stored Procedures.

writable()

See https://docs.python.org/3/library/io.html#io.IOBase.writable (https://docs.python.org/3/library/io.html#io.IOBase.writable)

write(b)

See https://docs.python.org/3/library/io.html#io.RawIOBase.write (https://docs.python.org/3/library/io.html#io.RawIOBase.write)

writelines(lines)

From https://docs.python.org/3/library/io.html#io.IOBase.writelines (https://docs.python.org/3/library/io.html#io.IOBase.writelines)

Attributes

语言: 中文