简体   繁体   中英

How to find files and save them to different folder in python

I amt trying to find all relevant files and folders from local repository. eg I pulled projects from git to my local machine and I want to search for specific files(.txt) and save them to new folder in same pattern (root->dir-> file). Basically I want to get rid of any files which doesn't match name and extension but keep the same format. Thanks in advance.

You can traverse the directory tree finding the files you want with this:

import os, fnmatch

def find_files(directory, pattern):
    for root, dirs, files in os.walk(directory):
        for basename in files:
            if fnmatch.fnmatch(basename, pattern):
                filename = os.path.join(root, basename)

                yield filename

def find_files_to_list(directory, pattern):
    file_list = []
    for root, dirs, files in os.walk(directory):
        for basename in files:
            if fnmatch.fnmatch(basename, pattern):
                filename = os.path.join(root, basename)
                file_list.append(filename)

    return file_list

then copy the repository and do something like this:

wanted_files = find_files_to_list('/original_project/', '*.html')
for filename in find_files('/copy_project/', '*'):
    if filename not in wanted_files:
        os.remove(filename)

you can do this:

import shutil
import os 

The path of the folder where the files are:

path = r'C:\Users\pc\Downloads\6.ahmed'

code to select specific files, here I'm selecting all files in the folder with.xml extension. You can change it according to your need.

xml_files = [f for f in os.listdir(path) if f.endswith('.xml')]

source and destination folder path and the loop to move the files:

source_folder = r"C:\Users\pc\Downloads\6.ahmed\\"
destination_folder = r"C:\Users\pc\Downloads\annotations\\"


# iterate files
for file in xml_files:
    # construct full file path
    source = source_folder + file
    destination = destination_folder + file
    # move file
    shutil.move(source, destination)
    print('Moved:', file)

combine the whole code and run it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM