Python Forum
get all .exe files from folder and subfolder and then copy them to another location - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: get all .exe files from folder and subfolder and then copy them to another location (/thread-11669.html)



get all .exe files from folder and subfolder and then copy them to another location - evilcode1 - Jul-20-2018

hello folk ...
i am trying to write a code to get all .exe files from a dir by using os.walk .. then i need it to move this files to another location ...

this is my code :
import os
import shutil

for root, dirs, files in os.walk("/home/evilcode1/Desktop/My Files/USB/test/"):
    for file in files:
        if file.endswith(".exe"):
             print(os.path.join(root, file))
             
print( "start copy ... " )
can u help me how to move this files to another location

thank u brothers i did it :

import os
import shutil


source = "/home/evilcode1/Desktop/My Files/USB/test/"
dist   = "/home/evilcode1/Desktop/py"

for root, dirs, files in os.walk(source):
    for file in files:
        if file.endswith(".exe"):
             q = os.path.join(root, file)
             print q + " ------> copy done"
             
             shutil.copy2(q , dist)
             


print( "Finish" )



RE: get all .exe files from folder and subfolder and then copy them to another location - DeaD_EyE - Jul-20-2018

Little improvement with pathlib.

import shutil
from pathlib import Path

source = "~/.wine"
# https://docs.python.org/3/library/pathlib.html#pathlib.Path.glob
source = Path(source).expanduser().glob('**/*.exe')
destination   = Path("~/BACKUP/").expanduser()

for source_file in source:
    shutil.copy2(source_file , destination)
    print(f'{source_file.name:40} --> {destination}')
    # https://pyformat.info/

print( "Finish" )