简体   繁体   English

在 Python 文件中包含所需字符串的目录中查找文件,并将所有这些文件存储到另一个文件夹中

[英]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.我们如何在同一目录中识别所需标签的 XML 文件,然后将它们分开到另一个目录中,例如,我有猫,狗,大象,斑马数据集的 XML 文件,但我想识别 Z38301BB1093B936 文件中名为“猫”的标签然后我希望所有属于“cat”label 的 XML 文件从该目录中剪切以将其粘贴到另一个目录中。 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:这是一种使用glob查找包含输入单词的目录中的所有 xml 文件的方法,然后检查新目录是否存在并移动文件:

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 查找包含所需文本的目录并将所有文件存储到另一个文件夹中 - Find a directory containing desired text and stored all that files into another folder 在 Python 中查找包含所需字符串的目录中的文件 - Find files in a directory containing desired string in Python 在文件夹中查找包含给定文件中的字符串的所有文件(Python) - Find all files in folder containing strings from given file (Python) Python:搜索特定字符串的文件文件夹,然后列出包含它的所有文件 - Python: Scour a folder of files for a specific string, then list all the files containing it 在 Python 中找到包含所需字符串的许多文件中的一个 - Find one file out of many containing a desired string in Python 用python如何从目录和子目录中的所有文件中查找字符串 - with python how to find a string from all files in a directory and subfloders Python-读取另一个文件夹/目录中的文本文件 - Python - Reading text files in another folder/directory 如何在Python中的目录中查找具有特定文件扩展名的所有文件 - How to find all files with specific file extensions in a directory in Python 根据另一个文件中的字符串重命名文件夹中的文件 - 使用 Python - Renaming files in a folder according to string in another file - with Python Python:使用包含相同文件的另一个目录覆盖目录 - Python: Overwriting a directory with another directory containing the same files
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM