Synthèse
Creates an AIO object to handle local and cloud files, and data stores in a unified way that can be used in Python.
Using this object, you can perform tasks such as copying files from one location to another and renaming or deleting a folder, as well as filing related operations including read and write. The AIO object can be used for cloud stores by specifying the path to an .acs file as the input. For a local data store, create an AIO object by specifying a local folder path, which will then be used as the current working directory, or by not specifying any parameter values.
Discussion
The AIO object supports file operations agnostic of the file system. The object currently supports Windows, Linux and major cloud storage implementations.
Apart from a local file system and UNC paths, the AIO object supports the following virtual cloud URIs for class initialization:
- ACS Connection files (.acs)—Stores connection information in binary persisted encrypted format. See the Create Cloud Storage Connection File tool for more information.
- /cloudStores json—Stores connection information in JSON format for ArcGIS Enterprise.
- /vsi paths—GDAL is the preferred URI for file systems in agnostic file operations. See GDAL Virtual File Systems for more information.
- http[s] paths—The cloud provider URL for a resource, which sometimes contains connection information such as Microsoft Azure SAS.
- A cloud provider-specific URI schema prefix such as s3:// or abfs://.
Remarque :
Connection information can also be used through IAM/RBAC, GDAL environment options, and provider-specific CLI configuration files in the .aws, .azure, or bin/gdalrc folders.
All cloud-specific methods can be accessed using the cloud property.
When working with cloud operations, class methods support relative paths to the current working directory (aio.getcwd). If absolute paths are used, only vsi paths are supported. These types of operations return vsi paths by default. The type of output path can be overridden by providing the CloudPathType enum.
These methods follow the same conventions and behavior as Python native file system methods.
Syntaxe
AIO ({path})
Paramètre | Explication | Type de données |
path | To construct an AIO object to handle a cloud store, use the .acs file path. To construct an AIO object to handle a local file store, this parameter can be a local folder path or it can be left empty. If a local folder path is provided, it will be used as the current working directory. | String |
Propriétés
Propriété | Explication | Type de données |
cloud (Lecture seule) | An instance of a class that has cloud-specific methods. It is used to retrieve details of the cloud store that was used when the AIO object was created. Returns a CloudOp object.
| Object |
Vue d’ensemble des méthodes
Méthode | Explication |
chdir (path) | Changes the working directory relative to the current working directory.. |
close (handle) | Closes the open file handle. |
copy (src, dst, {options}) | Copies a file from the source to the destination. For a cloud-based AIO object:
For a local AIO object:
|
copytree (src, dst, {options}) | Copies the contents from one directory to another. For a cloud-based AIO object:
For a local AIO object:
|
exists (path) | Verifies whether a path exists. |
getatime (path) | Gets the file access time for a file. |
getctime (path) | Gets the file creation time for a file. |
getcwd ({type}) |
Gets the current working directory. |
getmtime (path) |
Gets the file modification time for a file. |
getpath (path, {type}) |
Constructs a URI path. |
getsize (path) | Gets the size of a file in bytes. |
isdir (path) | Verifies whether a path is a directory. |
isfile (path) | Verifies whether a path is a file. |
listdir ({path}, {type}) |
Lists the contents of a directory nonrecursively. |
makedirs (path) | Creates a directory recursively. |
mkdir (path) | Creates a directory. |
open (path, {mode}, {encoding}, {mime}) | Opens a file handle. The handle must be explicitly closed. |
remove (path) | Deletes a file. |
removefiles (path) | Delete multiple files. |
rename (src, dst) | Renames or moves a file or a directory. Renaming cloud files is computationally intensive, as copy and delete are performed on the server side. For a cloud based AIO object:
For a local AIO object:
|
rmtree (path) | Removes a directory and its contents recursively. |
scandir (path, {depth}, {type}) |
Returns an iterable of entries in a directory. |
Méthodes
chdir (path)
Paramètre | Explication | Type de données |
path | A relative path from the current working directory or an absolute path (absolute vsi path for cloud). | String |
cloud_io = AIO(r"C:\data\datacloud.acs")
cloud_io.chdir(r"aio") # relative path
cloud_io = AIO(r"C:\data\datacloud.acs")
cloud_io.chdir(r"/vsis3/u-agu/arcio") # absolute VSI path
local_io = AIO(r"C:\data")
local_io.chdir(r"aio")
close (handle)
Paramètre | Explication | Type de données |
handle | An open file handle. The data type is AIOFile. Remarque :An instance of this class is returned by the AIO object's open method. | Object |
cloud_io = AIO(r"C:\data\datacloud.acs")
rcsfile = cloud_io.open(r'testfile.txt', 'r')
rcsfile.read(size=4)
cloud_io.close(rcsfile)
copy (src, dst, {options})
Paramètre | Explication | Type de données |
src | The source path of the file. | String |
dst | The target path of the file. | String |
options | Specifies the options that will be used.
Syntax—RECURSIVE=[YES|NO], SYNC_STRATEGY=[TIMESTAMP|ETAG|OVERWRITE], NUM_THREADS=(integer) CHUNK_SIZE=(integer) [x-amz-*|x-goog-*|x-ms-*=(value)] Example—{'RECURSIVE':'YES', 'SYNC_STRATEGY':'OVERWRITE', 'NUM_THREADS':'10', 'CHUNK_SIZE':'8'} (La valeur par défaut est None) | Dictionary |
Type de données | Explication |
String | The destination path. |
cloud_io = AIO(r"C:\data\datacloud.acs")
cloud_io.copy(r'/vsis3/data/list/utf8_test.json',
r'/vsis3/data/list/new_dir_new/utf8_test.json')
local_io = AIO(r"C:\data")
local_io.copy(r"C:\data\datatest.json", r"C:\data_temp\datatest.json")
copytree (src, dst, {options})
Paramètre | Explication | Type de données |
src | The source path of the file. | String |
dst | The target path of the file. | String |
options | Specifies the options that will be used.
Syntax—RECURSIVE=[YES|NO], SYNC_STRATEGY=[TIMESTAMP|ETAG|OVERWRITE], NUM_THREADS=(integer) CHUNK_SIZE=(integer) [x-amz-*|x-goog-*|x-ms-*=(value)] Example— {'RECURSIVE':'YES', 'SYNC_STRATEGY':'OVERWRITE', 'NUM_THREADS':'10', 'CHUNK_SIZE':'8'} (La valeur par défaut est None) | Dictionary |
Type de données | Explication |
String | The destination path. |
cloud_io = AIO(r"C:\data\datacloud.acs")
cloud_io.copytree(r'/vsis3/data/list', r'/vsis3/data/data_new_dir')
local_io = AIO(r"C:\data")
local_io.copytree(r"C:\data\list", r"C:\data\data_new_dir")
exists (path)
Paramètre | Explication | Type de données |
path | A relative path from the current working directory or an absolute path (absolute vsi path for cloud). | String |
Type de données | Explication |
Boolean | True if the directory or file exists; otherwise, False. |
cloud_io = AIO(r"C:\data\datacloud.acs")
cloud_io.exists(r"C:\data\datacloud.acs\data")
getatime (path)
Paramètre | Explication | Type de données |
path | The path for which the access time will be obtained. The value can be a relative path from the current working directory or an absolute path. | String |
Type de données | Explication |
Double | The access time, in number of seconds since epoch. |
This method is not available for cloud storage.
local_io = AIO(r"C:\data")
local_io.getatime(r"data.json")
getctime (path)
Paramètre | Explication | Type de données |
path | The path for which the creation time will be obtained. The value can be a relative path from the current working directory or an absolute path. | String |
Type de données | Explication |
Double | The creation time, in number of seconds since epoch. |
This method is not available for cloud storage.
local_io = AIO(r"C:\data")
local_io.getctime(r"data.json")
getcwd ({type})
Paramètre | Explication | Type de données |
type | Specifies the path style for the returned URI (for cloud only). The data type is CloudPathType. Available options are CloudPathType.VSI, CloudPathType.ACS, CloudPathType.HTTP, and CloudPathType.CLOUDSTORES. (La valeur par défaut est CloudPathType.VSI) | Object |
Type de données | Explication |
String | The current working directory. |
local_io = AIO()
local_io.getcwd()
cloud_io = AIO(r"C:\data\datacloud.acs")
cloud_io.getcwd()
getmtime (path)
Paramètre | Explication | Type de données |
path |
The relative path from the current working directory or an absolute path (absolute vsi path for cloud). | String |
Type de données | Explication |
Double | The file modification time in number of seconds since epoch. |
local_io = AIO(r"C:\data")
local_io.getmtime(r"data.json")
cloud_io = AIO(r"C:\data\datacloud.acs")
cloud_io.getmtime(r"data.json")
getpath (path, {type})
Paramètre | Explication | Type de données |
path | The relative path from the current working directory or an absolute path (absolute vsi path for cloud). | String |
type | Specifies the path style for the returned URI (for cloud only). The data type is CloudPathType. Available options are CloudPathType.VSI, CloudPathType.ACS, CloudPathType.HTTP, and CloudPathType.CLOUDSTORES. (La valeur par défaut est CloudPathType.VSI) | Object |
Type de données | Explication |
String | The constructed URI path. |
from arcpy import CloudPathType
cloud_io = AIO(r"C:\data\datacloud.acs")
cloud_io.getpath('cog.tif', CloudPathType.HTTP)
getsize (path)
Paramètre | Explication | Type de données |
path | The relative path from the current working directory or an absolute path (absolute vsi path for cloud). | String |
Type de données | Explication |
Integer | The size of the file in bytes. |
cloud_io = AIO(r"C:\data\datacloud.acs")
cloud_io.getsize(r'cog.tif')
isdir (path)
Paramètre | Explication | Type de données |
path | A relative path from the current working directory or an absolute path (absolute vsi path for cloud). | String |
Type de données | Explication |
Boolean | True if the directory exists; otherwise, False. |
cloud_io = AIO(r"C:\data\datacloud.acs")
cloud_io.isdir(r'cog')
isfile (path)
Paramètre | Explication | Type de données |
path | A relative path from the current working directory or an absolute path (absolute vsi path for cloud). | String |
Type de données | Explication |
Boolean | True if the file exists; otherwise, False. |
cloud_io = AIO(r"C:\data\datacloud.acs")
cloud_io.isfile(r'cog')
listdir ({path}, {type})
Paramètre | Explication | Type de données |
path | The relative path from the current working directory or an absolute path (absolute vsi path for cloud). (La valeur par défaut est None) | String |
type | Specifies the path style for the returned URI (for cloud only). The data type is CloudPathType. Available options are CloudPathType.VSI, CloudPathType.ACS, CloudPathType.HTTP, and CloudPathType.CLOUDSTORES. (La valeur par défaut est CloudPathType.VSI) | Object |
Type de données | Explication |
String | A list of file names in the folder. |
cloud_io = AIO(r"C:\data\datacloud.acs")
cloud_io.listdir('data_folder')
from arcpy import CloudPathType
cloud_io = AIO(r"C:\data\datacloud.acs")
cloud_io.listdir('data_folder', CloudPathType.ACS)
local_io = AIO(r"C:\data")
local_io.listdir()
makedirs (path)
Paramètre | Explication | Type de données |
path | A relative path from the current working directory or an absolute path (absolute vsi path for cloud). | String |
cloud_io = AIO(r"C:\data\datacloud.acs")
cloud_io.makedirs(r"datasensor\datatest\data")
mkdir (path)
Paramètre | Explication | Type de données |
path | A relative path from the current working directory or an absolute path (absolute vsi path for cloud). | String |
cloud_io = AIO(r"C:\data\datacloud.acs")
cloud_io.mkdir(r'cog')
open (path, {mode}, {encoding}, {mime})
Paramètre | Explication | Type de données |
path | A relative path from the current working directory or an absolute path (absolute vsi path for cloud). | String |
mode | Specifies the mode that will be used for opening a file.
Remarque :Only read mode honors the encoding parameter. (La valeur par défaut est None) | String |
encoding | The type of encoding that will be used to open a file.
(La valeur par défaut est utf-8) | String |
mime | The Multipurpose Internet Mail Extensions (MIME) headers. It is recommended that you set MIME headers when creating the file for faster creation and setting of metadata. (La valeur par défaut est None) | Dictionary |
Type de données | Explication |
File | Returns the AIOFile object. |
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.")
remove (path)
Paramètre | Explication | Type de données |
path | A relative path from the current working directory or an absolute path (absolute vsi path for cloud). | String |
cloud_io = AIO(r"C:\data\datacloud.acs")
cloud_io.remove(r'rcsfile.txt')
local_io = AIO(r"c:\data")
local_io.remove(r'rcsfile.txt')
removefiles (path)
Paramètre | Explication | Type de données |
path [path,...] | The relative path from the current working directory or an absolute path (absolute vsi path for cloud). | String |
Type de données | Explication |
List | Returns the list of paths that were not removed in case of failure. |
cloud_io = AIO(r"C:\data\datacloud.acs")
cloud_io.removefiles(['/vsis3/data/list/to_be_deleted/datatest.json','/vsis3/u-agu/list/to_be_deleted/data_new.json'])
local_io = AIO(r"c:\data")
local_io.removefiles([r'C:\data\datatest.json',r'C:\data\data_new.json'])
rename (src, dst)
Paramètre | Explication | Type de données |
src | The source path of the file. | String |
dst | The target path of the file. | String |
cloud_io = AIO(r"C:\data\datacloud.acs")
cloud_io.rename(r'to_be_renamed/cog.tif',r'to_be_renamed/cognew.tif')
local_io = AIO(r"C:\data")
local_io.rename(r'test.json',r'new_test.json')
rmtree (path)
Paramètre | Explication | Type de données |
path | The relative path from the current working directory or an absolute path (absolute vsi path for cloud). | String |
cloud_io = AIO(r"C:\data\datacloud.acs")
cloud_io.rmtree(r"aio") # Relative path
cloud_io = AIO(r"C:\data\datacloud.acs")
cloud_io.rmtree(r"/vsis3/data/arcio") # Absolute VSI path
local_io = AIO(r"C:\data")
local_io.rmtree(r"aio")
scandir (path, {depth}, {type})
Paramètre | Explication | Type de données |
path | The relative path from the current working directory or an absolute path (absolute vsi path for cloud). | String |
depth | The recursion depth.
(La valeur par défaut est 0) | Integer |
type | Specifies the path style for the returned URI (for cloud only). The data type is CloudPathType. Available options are CloudPathType.VSI, CloudPathType.ACS, CloudPathType.HTTP, and CloudPathType.CLOUDSTORES. (La valeur par défaut est CloudPathType.VSI) | Object |
Type de données | Explication |
Iterable | An iterable of the AIODirEntry object. |
cloud_io = AIO(r"C:\data\datacloud.acs")
# Depth = 0, performs cur dir scanning only
from arcpy import CloudPathType
for item in cloud_io.scandir(r'list', depth=0, type=CloudPathType.HTTP):
print(item.path)
print(item.is_dir())
print(item.is_file())
ob = item.stat()
print(ob.st_mode)
# Cloud specific operations through cloud property
print(item.cloud.getvsipath())
print(item.cloud.getpath(CloudPathType.ACS))
local_io = AIO(r"c:\data")
# Depth = -1, performs recursive scanning
for item in local_io.scandir(r'aio', -1):
print(item.name)
print(item.path)
print(item.is_dir())
print(item.is_file())
ob = item.stat()
print(ob.st_mode)
Exemple de code
# To create an AIO object based on cloud store
cloud_io = AIO(r"C:\data\datacloud.acs")
cloud_io.getcwd()
cloud_io.listdir(r'aio')
# aio is a virtual folder in the cloud store referred in datacloud.acs
# To create an AIO object based on local file system
local_io = AIO()
local_io.getcwd() # Returns the current working directory
# To create an AIO object based on local file system with path parameter
local_io = AIO(r"C:\data") # Set the current working directory as C:\data
local_io.getcwd() # Return the current working directory that is C:\data
Vous avez un commentaire à formuler concernant cette rubrique ?