2

I have the following directory structure with the following files:

Folder_One
├─file1.txt
├─file1.doc
└─file2.txt
Folder_Two
├─file2.txt
├─file2.doc
└─file3.txt

I would like to get only the .txt files from each folder listed. Example:

Folder_One-> file1.txt and file2.txt
Folder_Two-> file2.txt and file3.txt

Note: This entire directory is inside a folder called dataset. My code looks like this, but I believe something is missing. Can someone help me.

path_dataset = "./dataset/"
filedataset = os.listdir(path_dataset)
    
    for i in filedataset:
        pasta = ''
        pasta = pasta.join(i) 
        for file in glob.glob(path_dataset+"*.txt"):
            print(file)
simonica
  • 97
  • 7

2 Answers2

5
from pathlib import Path

for path in Path('dataset').rglob('*.txt'):
    print(path.name)

Using glob

import glob
for x in glob.glob('dataset/**/*.txt', recursive=True):
    print(x)
bigbounty
  • 14,834
  • 4
  • 27
  • 58
1

You can use re module to check that filename ends with .txt.

import re
import os
path_dataset = "./dataset/"
l = os.listdir(path_dataset)

for e in l:
   if os.path.isdir("./dataset/" + e):
      ll = os.listdir(path_dataset + e)
      for file in ll:
          if re.match(r".*\.txt$", file):
              print(e + '->' + file)
ThunderPhoenix
  • 1,583
  • 4
  • 19
  • 45