> ## 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="Using 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="Using 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="Using 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="Using 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="Using 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="Using 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="Using 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="Using 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="Using 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="Using 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>
