简体   繁体   中英

Get files from specific folders in python

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)
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)

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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