0

I have a textfile containing a list of file paths, which I want to access with Python, copy and paste into a new folder.

The file path list looks like this:
filepath1
filepath2
...

All files should be copied and pasted into a new folder (output_folder). How can I achieve this?

My code so far:

for filename in textfile:
    text=filename.read()
    for line in text:
        line=filepath
          #move filepath?
Dominik Scheld
  • 125
  • 2
  • 9
  • 1
    Possible duplicate of [How to move a file in Python](https://stackoverflow.com/questions/8858008/how-to-move-a-file-in-python) – C.Nivs Nov 22 '18 at 17:14

1 Answers1

0

I think you can use the shutil.copy function to achieve your goal. It would be something like this:

import shutil
import os

absolute_path = os.getcwd() # Stores original path.

os.makedirs("/output_folder") # Creates output_folder. 

with open("filepaths.txt") as file:
    for filepath in file.readlines():
        path = filepath[:filepath.rfind("/")] # Extracts folder.
        os.chdir(path) # Changes directory.
        filename = filepath[filepath.rfind("/") + 1:] # Extracts filename.
        filename = filename.replace("\n","") # Gets rid of newline character.
        shutil.copy(filename, absolute_path + "/output_folder/")

If it raises PermissionDenied error, comment the line that creates the output folder and create it manually in the working directory.