Skip to main content

close

Краткая информация

Содержит методы для выполнения операций с файлами, такие как seek, tell, read и write.

Обсуждение

Этот класс не может быть запущен напрямую. Экземпляр этого класса возвращается методом 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.")