简体   繁体   中英

Find files in a directory containing desired string in Python file and stored all that files into another folder

How can we identify XML files of desired labels in the same directory and then separate them into another directory, for example, I have Cat, Dog, Elephant, zebra dataset's XML files but I want to identify the "Cat" named tags in XML files and then I want all that XML files belonging to "cat" label to cut from that directory to paste it into another directory. I tried a lot to find out the way to get proper results with the help of parse commands.

import os

user_input = input('What is the name of your directory')

directory = os.listdir(user_input)

searching = input('What word are you trying to find?')

for fname in directory:
    if os.path.isfile(user_input + os.sep + fname):
        # Full path
        f = open(user_input + os.sep + fname, 'r')

        if searching in f.read():
            print('found string in file %s' % fname)
        else:
            print('string not found')
        f.close()

Here's an approach that uses glob to find all xml files in the directory that contain the input word, then checks for the existence of the new directory and moves the files:

from glob import glob
import os
import shutil

directory = input('What is the name of your directory')
searching = input('What word are you trying to find?')
files = glob(f"{directory}/*{searching}*.xml")

if files:
    # check if directory already exists
    if not os.path.exists(searching):
        os.makedirs(searching)

    #move files to directory
    for file in files:
        shutil.move(file, directory)

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