> ## 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.

# Gérer les fichiers

> Dans cette section, vous trouverez des informations et des exemples sur la manipulation des fichiers associés à votre application. Apprenez à lister, lire, créer et supprimer des fichiers à l'aide de la classe Client ou Application pour une administration efficace des ressources de votre application.

[Client]: client

[Application]: client#application

Toutes les opérations ci-dessous peuvent être effectuées via la classe [Client], la
classe [Application] ou la CLI. Voici des exemples montrant comment réaliser chacune de
ces tâches avec les deux classes :

## Obtenir la liste des fichiers

`client.app_files_list` et `app.files_list` renvoient une liste d'objets `FileInfo`.

<Tabs>
  <Tab title="Avec le 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="Avec l'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>

## Lire un fichier

`client.read_app_file` et `app.read_file` renvoient un objet `BytesIO`.

<Tabs>
  <Tab title="Avec le 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="Avec l'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>

## Créer un fichier

`client.create_app_file` et `app.create_file` renvoient un objet `Response`.

<Tabs>
  <Tab title="Avec le 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="Avec l'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>

## Supprimer un fichier

`client.delete_app_file` et `app.delete_file` renvoient un objet `Response`.

<Tabs>
  <Tab title="Avec le 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="Avec l'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>

## Déplacer un fichier

`client.move_app_file` et `app.move_file` renvoient un objet `Response`.

<Tabs>
  <Tab title="Avec le 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="Avec l'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>
