Export the list of file name from CSV file and copy to another directory with python
<p>i have one csv file that include the list of file:
nome
Desert
Hydrangeas
Jellyfish
Koala
Lighthouse
Penguins
Tulips
I would like to create one script with python for copy this list of file from one directory to another directory, i can do this :</p>

<pre><code>import csv
import os
import shutil
source = os.listdir("C:\Test")
destination = "C:\Test1"
with open('semplice1.csv') as csvfile:
       reader = csv.DictReader(csvfile)
       for row in reader:
           print(row['nome'])
</code></pre>

<p>in this wway i can print the name of the file.
Can you help me to complete the code for copy only the list file?
Thanks.<code>enter code here</code></p>

<p>The issue is that <code>os.listdir()</code> returns a <em>list</em> of files contained in the directory that you give it - in this case "C:\test".</p>

<p>Later on, when you then try and concatenate <code>source</code> with the individual files, you are therefor attempting to combine a list with a str. This is why you are getting the <code>TypeError</code>.</p>

<p>If all of the files are contained directly in "C:\test", then you can drop the <code>os.listdir()</code> and do:</p>

<pre><code>import csv
import os
import shutil

source = "C:\Test"
destination = "C:\Test1"

with open('semplice1.csv') as csvfile:
       reader = csv.DictReader(csvfile)
       for row in reader:
           print(row['nome'])
           a = row['nome']
           shutil.copyfile(os.path.join(source, a), destination)
</code></pre>

<p>Note the use of <code>os.path.join()</code> also as a better solution for building filepaths up.</p>

python csv