Python – os.walk won't copy files from multiple folders

Question:

I have this script that hopefully moves all the files in multiple folders into a new folder. I used the os.walk and shtil.copy functions. However the script does not work.

Here is the script:

import os
import shutil


for root, dirs, filename in os.walk(r"C:UsersedwardOneDriveSuspensia Pictures"):
    MoveFrom = r"C:UsersedwardOneDriveSuspensia Pictures"
    MoveTo = r"C:UsersedwardOneDrivePics"
    shutil.copy(os.path.join(MoveFrom, filename), os.path.join(MoveTo, filename))

Here is the error I get:

TypeError: join() argument must be str, bytes, or os.PathLike object, not 'list'
Asked By: PyMan

||

Answers:

import os
import shutil
from pathlib import Path


for path, subdirs, files in os.walk(r"C:UsersedwardOneDriveSuspensia Pictures"):
    MoveFrom = r"C:UsersedwardOneDriveSuspensia Pictures"
    MoveTo = r"C:UsersedwardOneDrivePics"

    for name in files:
        shutil.copy(os.path.join(path, name), Path(MoveTo))

Answered By: Sachin Salve

As the os.walk documentation said,

filenames is a list of the names of the non-directory files in dirpath.
which means that the filename in your code is type of list and that is not acceptable type for join().

Here’s a possible way to solve it,

import os
import shutil


files: list
for root, dirs, files in os.walk(r"."):
    source_path = r"."
    target_path = r"../test"
    for file in files:
        if os.path.isfile(os.path.join(source_path)):
            shutil.copy(os.path.join(source_path, file), os.path.join(target_path, file))

One thing that you should consider is that the files from the result of os.walk would be the files in each folder under the root, which is recursive. So, this script only is able to handle the files in the depth 1.

For moving all the files in each of the folder under your specific directory, this script may work.

import os
import shutil


files: list
for root, dirs, files in os.walk(r"."):
    target_path = r"../test"
    for file in files:
        source_path = os.path.join(root, file)
        shutil.copy(source_path, os.path.join(target_path, file))
Answered By: josix
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.