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?

5
  • you could use shutil.copy in a for loop for this Commented Feb 4, 2020 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? Commented Feb 4, 2020 at 23:42
  • This is a duplicate of: stackoverflow.com/q/123198/11301900 Commented Feb 4, 2020 at 23:44
  • Does this answer your question? How do I copy a file in Python? Commented Feb 5, 2020 at 3: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. Commented Feb 5, 2020 at 3:05

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.