递归Python查找String Line Startswith



我必须在所有文件中递归递归找到所有行(以字符串" excel"开头((目录和子目录中(。我需要每个文件名所找到的行(例如::例如:: filename1:Line1建立...filename2:

line2建立...输出结果中的文件称为" logfile"如果没有建立的行,则未保存在logfile中。

import os
word="excel"
from os.path import join
for (dirname, dirs, files) in os.walk('/batch/'):
    for filename in files:
      thefile = os.path.join(dirname,filename)
         for line in files: 
           if line.startswith(word):
                    print (line)
                    print (thefile)

谢谢

您的代码只有一个小问题:最大的是您在文件名上循环而不是文件内容。

import os
word="excel"
from os.path import join
for (dirname, dirs, files) in os.walk('/batch/'):
    for filename in files:
        thefile = os.path.join(dirname, filename)
        with open(thefile) as f:
            for line in f:
                if line.startswith(word):
                    print (line)
                    print (thefile)

编辑:

import os
word="excel"
from os.path import join
with open('log_result.txt', 'w') as log_file:
    for (dirname, dirs, files) in os.walk('/tmp/toto'):
        for filename in files:
            thefile = os.path.join(dirname, filename)
            with open(thefile) as f:
                lines = [line for line in f if line.startswith(word)]
            if lines:
                log_file.write("File {}:n".format(thefile))
                log_file.writelines(lines)

这是固定的代码。您无需重新浏览相同的文件列表。OS.Walk((将返回目录中的所有子目录,您需要做的就是循环所有目录。

示例代码

import glob
import os
word="excel"
for (dirname, dirs, files) in os.walk("/batch/"):
    for file_ in files :
        if  file_.startswith(word):
                print(file_)
                print(os.path.join(dirname, file_))
    for dir_ in dirs :
        myfiles  = glob.glob(os.path.join(dirname,dir_))
        for myfile in myfiles:
            if  myfile.startswith(word):
                    print(myfile)
                    print(os.path.join(dirname,myfiles))

希望这有帮助

最新更新