hy9fesh
hy9fesh

Reputation: 661

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. The .txt files are in the subdirectories. Here is what I have in a .py so far:

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. I want to be able to loop from the directory and go through every subdirectory. Any suggestions are welcome.

Upvotes: 0

Views: 1457

Answers (1)

hy9fesh
hy9fesh

Reputation: 661

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"

Upvotes: 1

Related Questions