user15051990
user15051990

Reputation: 1895

Read text files from the list of paths python

I have list of text files and I want to read them and write them to the directory.

list_text=["c:\\users\\sva\\abc.txt", "c:\\users\\sva\\mno.txt"]

    for file in list_text:
        with open(file,'r') as data:
            txt_file = data.readlines()

    with open(txt_path,'w') as out:
        out.write(txt_file)

getting error: TypeError: write() argument must be str, not list

Upvotes: 1

Views: 241

Answers (2)

Picachieu
Picachieu

Reputation: 3782

The readlines method returns a list of lines from a file. The write method requires a string argument, but you're passing a list argument. You can write each of the individual lines instead:

for line in txt_file:
    out.write(line)

Upvotes: 2

Axxeption
Axxeption

Reputation: 293

You have to use read() and not readlines(), does this solve your problem?

Upvotes: 0

Related Questions