Agregando a un archivo

Recordemos los diferentes modos que se soportan para trabajar con archivos:

Modo

Operación

Ejemplo

'r'

Leer (el predeterminado)

with open('document.txt', 'r') as f:

'w'

Escribir

with open('document.txt', 'w') as f:

'a'

Añadir

with open('document.txt', 'a') as f:

'r+'

Leer y escribir

with open('document.txt', 'r+') as f:

'x'

Crear (error si ya existe)

with open('document.txt', 'x') as f:

Hay diferentes archivos presentes en la computadora. Se te pide que agregues algunas líneas a ellos.

La entrada contiene 2 líneas. La primera línea es el nombre del archivo. La segunda es el contenido que queremos agregar a ese archivo.

El programa necesita primero agregar la línea especificada al archivo y luego imprimir todo el contenido del archivo.

Entrada

doc.txt (contenido inicial)

Salida

doc.txt (contenido resultante)

doc.txt
This is the second line

This is the first line

This is the first line
This is the second line

This is the first line
This is the second line

Entrada

empty.txt (contenido inicial)

Salida

empty.txt (contenido resultante)

empty.txt
Adding some content

Adding some content

Adding some content

Entrada

multiline.txt (contenido inicial)

Salida

multiline.txt (contenido resultante)

multiline.txt
The final line!

This is
some very
very long
document
with
many
lines of text…

This is
some very
very long
document
with
many
lines of text…
The final line!

This is
some very
very long
document
with
many
lines of text…
The final line!

Constraints

Time limit: 2 seconds

Memory limit: 512 MB

Output limit: 1 MB

To check your solution you need to sign in
Sign in to continue