Python Forum
How to copy files from subfolders into one folder
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to copy files from subfolders into one folder
#4
You already know, I like the abstraction of pathlib.

import shutil
from pathlib import Path


src_path = Path("U:\\collections\\2019_input")
out_path = Path("U:\\collections\\2019_output")


for filename in src_path.rglob('*'):
    target_file = out_path / filename.name
    if target_file.exists():
        print('File', target_file, 'exists already. Skipping...')
        continue
    print(filename, target_file)
    # shutil.copy2(filename, target_file)
Code haven't been tested. You have to be careful, pathlib got some additions, which are not in older versions available.
I use Python 3.7.4.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
RE: How to copy files from subfolders into one folder - by DeaD_EyE - Aug-29-2019, 08:36 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Why is the copy method name in python list copy and not `__copy__`? YouHoGeon 2 299 Apr-04-2024, 01:18 AM
Last Post: YouHoGeon
  Copy Paste excel files based on the first letters of the file name Viento 2 472 Feb-07-2024, 12:24 PM
Last Post: Viento
  Compare folder A and subfolder B and display files that are in folder A but not in su Melcu54 3 592 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  Rename files in a folder named using windows explorer hitoxman 3 772 Aug-02-2023, 04:08 PM
Last Post: deanhystad
  Rename all files in a folder hitoxman 9 1,538 Jun-30-2023, 12:19 AM
Last Post: Pedroski55
  Create new folders and copy files cocobolli 3 1,516 Mar-22-2023, 10:23 AM
Last Post: Gribouillis
  Copy only hidden files and folders with rsync Cannondale 2 1,036 Mar-04-2023, 02:48 PM
Last Post: Cannondale
  How to loop through all excel files and sheets in folder jadelola 1 4,552 Dec-01-2022, 06:12 PM
Last Post: deanhystad
  python move folders and subfolders not working mg24 5 2,224 Nov-09-2022, 02:24 PM
Last Post: Larz60+
  python gzip all files from a folder mg24 3 4,089 Oct-28-2022, 03:59 PM
Last Post: mg24

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020