简体   繁体   English

如何识别 Python 中的目录和子目录中的 all.txt 文件?

[英]How do I identify all .txt files in a directory and subdirectories in Python?

I am trying to loop through all.txt files in a file directory that contains a bunch of subdirectories.我正在尝试遍历包含一堆子目录的文件目录中的 all.txt 文件。 The.txt files are in the subdirectories. .txt 文件位于子目录中。 Here is what I have in a.py so far:到目前为止,这是我在 a.py 中的内容:

import os
import csv
import re

csv_cont = []
directory = os.getcwd()
for root,dir,files in os.walk(directory):
    for file in files:

        if file.endswith(".txt"):
            f = open(file, 'r')
            file_content = f.read()

            
            title = re.search('Title:(.*)', file_content)
            if title:
                if title.group(1):
                    title = title.group(1)
                else:
                    title = "\n"

The issue with this is that I don't have.txt files in the directory, so I need to put the above Python file in the subdirectory with the text files for the above code to work.问题是我的目录中没有.txt 文件,所以我需要将上面的 Python 文件放在带有文本文件的子目录中,上面的代码才能工作。 I want to be able to loop from the directory and go through every subdirectory.我希望能够从目录和 go 循环通过每个子目录。 Any suggestions are welcome.欢迎任何建议。

This worked:这有效:

import os
import csv
import re
import glob

csv_cont = []
for file in glob.glob('**/*.txt', recursive=True):

        if file.endswith(".txt"):
            f = open(file, 'r')
            file_content = f.read()

            
            title = re.search('Title:(.*)', file_content)
            if title:
                if title.group(1):
                    title = title.group(1)
                else:
                    title = "\n"

暂无
暂无

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

相关问题 如何运行 cloc 来检查一个目录(包括子目录)的所有 python 文件,但忽略任何带有迁移一词的子目录? - How do I run cloc to check all python files of a directory including subdirectories but ignore any subdirectories that have the word migrations? 在Python中,如何查找目录下的所有文件,包括子目录中的文件? - In Python, how to find all the files under a directory, including the files in subdirectories? 如何使用python使用带有for循环的目录中的所有txt文件 - How do I use all the txt files from a directory with a for loop using python Python:我如何将output目录下的所有文本内容包含.txt文件作为一个列表? - Python: How do I output all the text contents of a directory containing .txt files as a list? 在python中,如何获取目录中所有文件的路径,包括子目录中的文件,但不包括子目录的路径 - In python, how to get the path to all the files in a directory, including files in subdirectories, but excluding path to subdirectories python读取目录和子目录中的所有文件 - python read all files in directory and subdirectories 在python中的目录和子目录中创建所有文件的列表 - Create a list of all files in directory and subdirectories in python Python计算目录及其所有子目录中的文件 - Python count files in a directory and all its subdirectories 如何使用递归列出目录和子目录中的现有文件? - how do i list existing files in a directory and subdirectories using recursion? 如何获取目录中的文件,包括所有子目录 - How to get files in a directory, including all subdirectories
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM