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

# Gestión de Archivos

> En esta sección encontrarás información y ejemplos sobre cómo manipular los archivos asociados a tu aplicación. Aprende a listar, leer, crear y eliminar archivos usando la clase Client o la clase Application para una administración eficiente de los recursos de tu aplicación.

[Client]: client

[Application]: client#application

Todas las operaciones a continuación pueden realizarse tanto con la clase [Client], la
clase [Application] como con la CLI. A continuación se muestran ejemplos de cómo realizar cada una de
estas tareas usando ambas clases:

## Obteniendo una lista de archivos

`client.app_files_list` y `app.files_list` devuelven una lista de objetos `FileInfo`.

<Tabs>
  <Tab title="Usando 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="Usando 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>

## Leyendo un archivo

`client.read_app_file` y `app.read_file` devuelven un objeto `BytesIO`.

<Tabs>
  <Tab title="Usando 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="Usando 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>

## Creando un archivo

`client.create_app_file` y `app.create_file` devuelven un objeto `Response`.

<Tabs>
  <Tab title="Usando 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="Usando 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>

## Eliminando un archivo

`client.delete_app_file` y `app.delete_file` devuelven un objeto `Response`.

<Tabs>
  <Tab title="Usando 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="Usando 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>

## Moviendo un archivo

`client.move_app_file` y `app.move_file` devuelven un objeto `Response`.

<Tabs>
  <Tab title="Usando 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="Usando 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>
