Skip to content
Advertisement

getting TypeError: write() argument must be str, not list when trying to add text from 1 file to another

I got 2 text files with lists text1.txt

JavaScript

and text2.txt

JavaScript

What I want to do is add the text of file 1 after the second line of file 2 like this:

JavaScript

I wrote a the following script as suggested on this site:

JavaScript

and getting the following error:

JavaScript

Advertisement

Answer

When you do

JavaScript

you’re creating a nested list. t2 looks like

JavaScript

writelines() expects a flat list of strings, when it gets to the nested list it can’t write it.

If you want to splice into the list rather than inserting a nested list, use a slice assignment.

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement