简体   繁体   English

在Python中使用os.walk()模块从子文件夹中提取行?

[英]extract lines from sub-folders using os.walk() module in Python?

I want to open a series of sub-folders in a folder and find some text files and print some lines of the text files. 我想在文件夹中打开一系列子文件夹,找到一些文本文件并打印一些文本文件行。 I am using this: 我正在使用这个:

from glob import glob
import fileinput
with open('output.txt', 'w') as out:
    for line in fileinput.input(glob('*.')):
        if 'Subject:' in line:
            out.write(line)

This working perfectly only with in one folder but this cannot access the sub-folders as well.so i heard about the os.walk() module. 这只能在一个文件夹中正常工作,但也无法访问子文件夹。所以我听说了os.walk()模块。 Does anyone know how I can use the os.walk() module to to access sub-folders and extract and paste a particular line in a separate txt file ? 有谁知道我如何使用os.walk()模块访问子文件夹并将特定行提取并粘贴到单独的txt文件中?

Using os.walk and generator expression to get all file paths in current directory recursively: 使用os.walkgenerator表达式递归获取当前目录中的所有文件路径:

from glob import glob
import fileinput
import os

with open('output.txt', 'w') as out:
    files = (os.path.join(p, f) for p, ds, fs in os.walk(os.curdir) for f in fs)
    for line in fileinput.input(files):
        if 'Subject:' in line:
            out.write(line)

fs in the above code is a list of file names. 上面的代码中的fs是文件名的列表。 You need to iterate them to get file paths. 您需要对其进行迭代以获取文件路径。

os.path.join is used to make a path by joining parent directory p and file name f . os.path.join用于通过连接父目录p和文件名f来创建路径。

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

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