> ## Documentation Index
> Fetch the complete documentation index at: https://docs.squarecloud.app/llms.txt
> Use this file to discover all available pages before exploring further.

# 管理文件

> 在本节中，你将了解如何操作与应用关联的文件的相关信息和示例。学习如何使用 Client 或 Application 类列出、读取、创建和删除文件，从而高效地管理应用资源。

[Client]: client

[Application]: client#application

以下所有操作既可以通过 [Client] 类，也可以通过 [Application] 类以及 CLI 执行。下面是使用这两个类执行每项任务的示例：

## 获取文件列表

`client.app_files_list` 和 `app.files_list` 返回一个 `FileInfo` 对象列表。

<Tabs>
  <Tab title="使用 Client">
    ```python theme={null}
    import squarecloud as square

    client = square.Client(api_key='API KEY')

    async def example():
        files_list = await client.app_files_list(app_id='application_id', path='/')

        for file in files_list:
            print(file.name)  # 'main.py'
            print(file.type)  # 'directory' or 'file'
            print(file.size)  # 2140
            print(file.lastModified)  # 1677112835000
    ```
  </Tab>

  <Tab title="使用 Application">
    ```python theme={null}
    import squarecloud as square

    client = square.Client(api_key='API KEY')

    async def example():
        app = await client.app('application_id')
        files_list = await app.files_list(path='/')  # list[FileInfo(...)]

        for file in files_list:
            print(file.name)  # 'main.py'

            print(file.type)  # 'directory' or 'file'

            print(file.size)  # 2140

            print(file.lastModified)  # 1677112835000
    ```
  </Tab>
</Tabs>

## 读取文件

`client.read_app_file` 和 `app.read_file` 返回一个 `BytesIO` 对象。

<Tabs>
  <Tab title="使用 Client">
    ```python theme={null}
    import squarecloud as square

    client = square.Client(api_key='API KEY')

    async def example():
        file_bytes = await client.read_app_file(
            app_id='application_id', path='main.py'
        )

        print(file_bytes)  # b'01101000 01101001'
    ```
  </Tab>

  <Tab title="使用 Application">
    ```python theme={null}
    import squarecloud as square

    client = square.Client(api_key='API KEY')

    async def example():
        app = await client.app('application_id')
        file_bytes = await app.read_file(path='main.py')

        print(file_bytes)  # b'01101000 01101001'
    ```
  </Tab>
</Tabs>

## 创建文件

`client.create_app_file` 和 `app.create_file` 返回一个 `Response` 对象。

<Tabs>
  <Tab title="使用 Client">
    ```python theme={null}
    import squarecloud as square

    client = square.Client(api_key='API KEY')

    async def example():
        await client.create_app_file(
            app_id='application_id', path='/file.txt', file=square.File('file.txt')
        )
    ```
  </Tab>

  <Tab title="使用 Application">
    ```python theme={null}
    import squarecloud as square

    client = square.Client(api_key='API KEY')

    async def example():
        app = await client.app('application_id')

        await app.create_file(path='/file.txt', file=square.File('file.txt'))
    ```
  </Tab>
</Tabs>

## 删除文件

`client.delete_app_file` 和 `app.delete_file` 返回一个 `Response` 对象。

<Tabs>
  <Tab title="使用 Client">
    ```python theme={null}
    import squarecloud as square

    client = square.Client(api_key='API KEY')

    async def example():
        await client.delete_app_file(app_id='application_id', path='/file.txt')
    ```
  </Tab>

  <Tab title="使用 Application">
    ```python theme={null}
    import squarecloud as square

    client = square.Client(api_key='API KEY')

    async def example():
        app = await client.app('application_id')

        await app.delete_file(path='/file.txt')
    ```
  </Tab>
</Tabs>

## 移动文件

`client.move_app_file` 和 `app.move_file` 返回一个 `Response` 对象。

<Tabs>
  <Tab title="使用 Client">
    ```python theme={null}
    import squarecloud as square

    client = square.Client(api_key='API KEY')


    async def example():
        await client.move_app_file(
            app_id='application_id',
            origin='path/to/origin/file.py',
            dest='path/to/destination/file.py',
        )
    ```
  </Tab>

  <Tab title="使用 Application">
    ```python theme={null}
    import squarecloud as square

    client = square.Client(api_key='API KEY')


    async def example():
        app = await client.app('application_id')
        await app.move_file(
            origin='path/to/origin/file.py', dest='path/to/destination/file.py'
        )
    ```
  </Tab>
</Tabs>
