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
#1
Hi Smile
I want to copy tif files contained in subfolders of folder "U:\\collections\\2019_input\\" in one separated output folder "U:\\collections\\2019_output\\" (no subfolders needed in the output).
My code :
input = "U:\\collections\\2019_input\\"
output = "U:\\collections\\2019_output\\"

for dirpath, dirnames, filenames in os.walk(input):
    for filename in filenames:
        if filename.endswith(".tif"):
            shutil.copy2(filename, output)
But it seems that shutil.copy2 command does not find the file to be copied.
Any idea?
Thanks !
Reply
#2
filename is just the name of the file, without path. You need to add path

import os
import shutil
src_path = "U:\\collections\\2019_input\\" # don't use input - it's builtin functiom
out_path = "U:\\collections\\2019_output\\"
 
for dirpath, dirnames, filenames in os.walk(in_path):
    for filename in filenames:
        if filename.endswith(".tif"):
            src = os.path.join(dir_path, filename)
            dest = os.path.join(out_path, filename)
            shutil.copy2(src, dest)
other way is to use glob

import os
import glob
import shutil
src_path = "U:\\collections\\2019_input\\" # don't use input - it's builtin functiom
out_path = "U:\\collections\\2019_output\\"
  
for filename in glob.glob(os.path.join('.', '*.*')):
    _, fname = os.path.split(filename)
    shutil.copy2(filename, os.path.join(dest_path, fname))
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
This is great, buran, thank you very much !
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Why is the copy method name in python list copy and not `__copy__`? YouHoGeon 2 249 Apr-04-2024, 01:18 AM
Last Post: YouHoGeon
  Copy Paste excel files based on the first letters of the file name Viento 2 423 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 530 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  Rename files in a folder named using windows explorer hitoxman 3 735 Aug-02-2023, 04:08 PM
Last Post: deanhystad
  Rename all files in a folder hitoxman 9 1,482 Jun-30-2023, 12:19 AM
Last Post: Pedroski55
  Create new folders and copy files cocobolli 3 1,443 Mar-22-2023, 10:23 AM
Last Post: Gribouillis
  Copy only hidden files and folders with rsync Cannondale 2 997 Mar-04-2023, 02:48 PM
Last Post: Cannondale
  How to loop through all excel files and sheets in folder jadelola 1 4,460 Dec-01-2022, 06:12 PM
Last Post: deanhystad
  python move folders and subfolders not working mg24 5 2,156 Nov-09-2022, 02:24 PM
Last Post: Larz60+
  python gzip all files from a folder mg24 3 3,978 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