-1

I want to automate copying directory contents to another folder. I found this post (Copy directory contents into a directory with python) and would like to make it so that I can run a for loop. This is my code so far, however I am getting an error saying cannot copy tree because 'X' is not a directory where 'X' is the filepath of the directory I want to copy.

I tried running copy_tree once manually by simply copying the first value from my imported file list and it works. Where did I go wrong? Thanks.

from distutils.dir_util import copy_tree
import os
location = 'C:/users/trinhsk/desktop/out_space.txt'
with open(location,'r') as f:
    fromDirectory = f.readlines()


for i in fromDirectory:
    bsname = os.path.basename(os.path.dirname(os.path.dirname(i)))
    copy_tree(str(i), "H:/spectraDB_copy/{}/".format(bsname))
Spencer Trinh
  • 743
  • 12
  • 31

1 Answers1

0

The strings returned by readlines() have an EOL character ('\n') at the end. Try stripping the line before using it.

for i in fromDirectory:
    i = i.strip()
    bsname = os.path.basename(os.path.dirname(i))
    copy_tree(str(i), "H:/spectraDB_copy/{}/".format(bsname))
wwii
  • 23,232
  • 7
  • 37
  • 77