Python Forum
get all .exe files from folder and subfolder and then copy them to another location
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
get all .exe files from folder and subfolder and then copy them to another location
#1
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" )
Reply
#2
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" )
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 239 Apr-04-2024, 01:18 AM
Last Post: YouHoGeon
  Copy Paste excel files based on the first letters of the file name Viento 2 420 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 525 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  Rename files in a folder named using windows explorer hitoxman 3 733 Aug-02-2023, 04:08 PM
Last Post: deanhystad
  Rename all files in a folder hitoxman 9 1,480 Jun-30-2023, 12:19 AM
Last Post: Pedroski55
  Create new folders and copy files cocobolli 3 1,439 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,457 Dec-01-2022, 06:12 PM
Last Post: deanhystad
  python gzip all files from a folder mg24 3 3,971 Oct-28-2022, 03:59 PM
Last Post: mg24
  delete all files and subdirectory from a main folder mg24 7 1,581 Oct-28-2022, 07:55 AM
Last Post: ibreeden

Forum Jump:

User Panel Messages

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