Merge Into One

Python for AIMedium+10 XP+10

Merge Into One

Three people each wrote part of a story in their own file. Time to put it all in one place.

Write merge_files(source_paths, target_path):

  1. Read each file in source_paths, in the order given.
  2. Write all of their text, one after another, into the file called target_path.
  3. Read target_path back and return what it holds.
  • Don't add anything between the pieces. Just put them side by side.
  • If source_paths is empty, the target file should be empty.
  • Use with for every file you open.

Leave the make_file helper as it is.

Examples

source_paths = [make_file('p1.txt', 'Once '), make_file('p2.txt', 'upon a time')]
target_path = 'story.txt'
returns 'Once upon a time'
source_paths = [make_file("mg_a1.txt", "ab"), make_file("mg_a2.txt", "cd")]
target_path = "mg_a_out.txt"
returns 'abcd'
source_paths = [make_file("mg_b1.txt", "only one")]
target_path = "mg_b_out.txt"
returns 'only one'

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run