0

I have a text file and I want to write it to a list of file paths I have saved in a numpy array. The code looks similar to this:

import numpy as np

img_file_path = ['/home/myname/testfolder/folder1', '/home/myname/testfolder/folder2', '/home/myname/testfolder/folder3'] #list of file paths
img_file_path = np.array(img_file_path) 
img_file_path.astype(str) #numpy array of file paths

constraints = open('/home/myname/Constrain.single')

My list of folder paths is much longer but I would like to save the file Constraints.single to each folder in my list of file paths. Is there any way to do this so I don't have to go through and paster the folder in manually?

  • you could use `shutil.copy` in a for loop for this – Dan Feb 04 '20 at 15:40
  • What is the issue, exactly? Have you tried anything, done any research? How is this not a duplicate of the many questions on copying files? Why are you holding the file names in a NumPy array? – AMC Feb 04 '20 at 23:42
  • This is a duplicate of: https://stackoverflow.com/q/123198/11301900 – AMC Feb 04 '20 at 23:44
  • Does this answer your question? [How do I copy a file in Python?](https://stackoverflow.com/questions/123198/how-do-i-copy-a-file-in-python) – AMC Feb 05 '20 at 03:04
  • Yeah, I double checked, and I’m sure this is a duplicate. The fact that the same operation is repeated using a loop does not mean it should be an entirely new/separate question. – AMC Feb 05 '20 at 03:05

1 Answers1

1
import shutil

img_file_path = ['/home/myname/testfolder/folder1', '/home/myname/testfolder/folder2',
                 '/home/myname/testfolder/folder3']
src = '/home/myname/Constrain.single'

for path in img_file_path:
    shutil.copy(src, path)
Dan
  • 1,575
  • 1
  • 11
  • 17