Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:\Users\edward\OneDrive\Suspensia Pictures"):
    MoveFrom = r"C:\Users\edward\OneDrive\Suspensia Pictures"
    MoveTo = r"C:\Users\edward\OneDrive\Pics"
    shutil.copy(os.path.join(MoveFrom, filename), os.path.join(MoveTo, filename))

Here is the error I get:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

import os
import shutil
from pathlib import Path


for path, subdirs, files in os.walk(r"C:\Users\edward\OneDrive\Suspensia Pictures"):
    MoveFrom = r"C:\Users\edward\OneDrive\Suspensia Pictures"
    MoveTo = r"C:\Users\edward\OneDrive\Pics"

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading