Skip to main content

close

汇总

包含执行文件相关操作的方法,例如 seektellreadwrite

讨论

此类无法直接实例化。 由 AIO 对象的 open 方法返回此类的实例。

语法

close()

属性

名称 说明 数据类型

cloud

(只读)

具有特定于云的方法的类实例。 创建 AIOFile 对象时,用于检索所使用的云存储的详细信息。

返回一个 CloudFileOp 对象。

cloud_io = AIO(r"C:\data\datacloud.acs")
rcsfile = cloud_io.open(r'testfile.txt', 'r')
rcsfile.cloud.clearcache()

Object

方法

close()

刷新并关闭打开的文件句柄。

rcsfile = cloud_io.open(r'cog.txt', 'r')
rcsfile.close()

flush()

刷新文件的写入缓冲区。

rcsfile = cloud_io.open(r'cog.txt', 'r')
rcsfile.flush()

read(size)

从对象中读取数据,并根据打开的项目返回二进制或文本。

名称 说明 数据类型

size

The number of bytes that will be read. A value of -1 reads all the content.

Integer

返回值

数据类型 说明

String

根据打开的项目返回二进制或文本(字节或字符串)。

from arcpy import AIO
cloud_io = AIO(r"C:\data\datacloud.acs")
rcsfile = cloud_io.open(r'testfile.txt', 'r')
rcsfile.read(size=4)
rcsfile.close()
from arcpy import AIO
cloud_io = AIO(r"C:\data\datacloud.acs")
with cloud_io.open(r'testfile.txt', 'r') as rcsfile:
    rcsfile.read(size=4)

rewind()

将流的位置重置为起始位置。

cloud_io = AIO(r"C:\data\datacloud.acs")
rcsfile = cloud_io.open(r'testfile.txt', 'r')
rcsfile.rewind()

seek(offset, {whence})

将流的位置更改为指定的字节偏移。

名称 说明 数据类型

offset

The number of bytes that will be read. A value of -1 reads all the content.

(默认值为 -1)

Integer

whence

(可选)

Specifies how the file position indicator will be set.

  • os.SEEK_SET—The position will be set relative to the beginning of the file.

  • os.SEEK_CUR—The position will be set relative to the current file position.

  • os.SEEK_END—The position will be set relative to the end of the file.

A nonzero value for os.SEEK_CUR or os.SEEK_END is not supported for local files when the file is opened in a nonbinary mode.

Object

import os
rcsfile = cloud_io.open(r'cog.txt', 'r')
rcsfile.seek(offset=6, whence=os.SEEK_CUR)

tell()

返回当前流位置。

返回值

数据类型 说明

Integer

当前流位置。

rcsfile = cloud_io.open(r'cog.tif', 'rb')
print(rcsfile.tell())

write(b)

将数据写入本地文件或云对象。

名称 说明 数据类型

b

The data that will be written.

String

返回值

数据类型 说明

Integer

写入的字节数。

from arcpy import AIO
local_io = AIO(r"C:\data")
rcsfile = local_io.open(r'testfile.txt', 'r')
rcsfile.write("This is a test file.")
rcsfile.close()
from arcpy import AIO
cloud_io = AIO(r"C:\data\datacloud.acs")
rcsfile = cloud_io.open(r"C:\data\info\datafile.txt", 'w', mime={'Content-Type': 'text/plain'})
rcsfile.write("This is a test file.")
rcsfile.close()
# Using with statement (context manager to close the opened file)
cloud_io = AIO(r"C:\data\datacloud.acs")
with cloud_io.open(r"C:\data\info\datafile.txt", 'w', mime={'Content-Type': 'text/plain'}) as rcsfile:
    rcsfile.write("This is a test file.")