简体   繁体   English

在文件夹和子文件夹中搜索多个文件 python

[英]Search multiple files in folder and sub folders python

Trying to search for a search word inside multiple files in current folder and sub folders and print them to a file.尝试在当前文件夹和子文件夹的多个文件中搜索搜索词并将它们打印到文件中。

i can print out the files in terminal using os.walk.我可以使用 os.walk 打印出终端中的文件。 But i want to search all the files in the folders for a specific word但我想在文件夹中的所有文件中搜索特定单词

    import glob, os
    
   output_file = 'Funkar.txt'
search_str = '"Prefix"'

for root, dirs, files in os.walk("."):
    for file in files:
        if file.endswith(".wpp"):
             print(os.path.join(root, file))

        with  open(os.path.join(root, file), 'r') as fr, open (output_file,'a',  encoding='utf-8') as fw:
            for i,line in enumerate(fr):
                if line.find(search_str) != -1: 
                    fw.write(line)

now gives me现在给我

in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 12: character maps to <undefined>

Search and print:搜索和打印:

import os
def search( folder, word ):
    for root, dirs, files in os.walk(folder):
        path = root.split(os.sep)
        for filename in files:
            fullname = os.path.join(root, filename)
            if open( fullname ).read().find( word ) != -1:
                print( fullname ) 

search( "FolderName", 'any text')

Search and backup searched lines:搜索和备份搜索到的行:

import os
def search( folder, word, bfile ):
    for root, dirs, files in os.walk(folder):
        path = root.split(os.sep)
        for filename in files:
            fullname = os.path.join(root, filename)
            if open( fullname ).read().find( word ) != -1:
                print( fullname ) 
                blines = [x for x in open(fullname).readlines() if x.find(word) != -1]
                open( bfile, 'a' ).writelines(blines) 

search( "FolderName", 'any text', 'backup-lines.file')

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

相关问题 在文件夹和子文件夹中搜索以条件开头的文件 - Search a folder and sub folders for files starting with criteria 如何使用Python合并一个文件夹及其子文件夹中的多个Excel文件 - How to merge multiple Excel files from a folder and it's sub folders using Python 从 python 文件夹中的子文件夹中读取 excel 文件 - read excel files from sub folders in a folder in python 如何使用 Python 将子文件夹和文件复制到新文件夹中 - How to Copy Sub Folders and files into New folder using Python 使用 Python 将内容从单个文件夹拆分到多个子文件夹 - Splitting content from a single folder to multiple sub folders using Python 如何在包含许多子文件夹和文件的python文件夹中检索某些文件 - How to retrieve certain files inside a folder containing many sub-folders and files python 如何将特定文件从子文件夹复制到python中的新文件夹? - How to copy specific files from the sub-folders to a new folder in python? 合并来自不同文件夹的多个文件,并通过 Python 将文件夹名称重命名为新文件夹 - Combine multiple files from different folders and renamed as folder name to new folder by Python 使用python脚本将文件分离到文件夹和子文件夹 - Seperate files to folders and sub folders using python script 替换 python 中的多个文件和文件夹 - Replace multiple files and folders in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM