Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8380415
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:27:10+00:00 2026-06-09T16:27:10+00:00

import os import glob for root, dirs, files in os.walk(‘F:\\workspace’): for txtfile in glob.iglob(os.path.join(root,’*.txt’)):

  • 0
import os
import glob

for root, dirs, files in os.walk('F:\\workspace'):
    for txtfile in glob.iglob(os.path.join(root,'*.txt')):
        print txtfile

I can get the files with no “[]”,and it can work if the folder name have only one of the symbols,”[” or “]”.
I think I am using glob.iglob in the wrong way; but I don’t know how to get the files I need without it.
Thanks for your reply.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T16:27:12+00:00Added an answer on June 9, 2026 at 4:27 pm

    The Python documentation for glob mentions that it uses fnmatch.fnmatch() behind the scenes.

    If you only want to match against the file name but not the folder name, try the following code:

    for root, dirs, files in os.walk('D:\\Scratch'):
      for file in files:
        if fnmatch.fnmatch(file, '*.txt'):
          print os.path.join(root, file)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

import glob list = glob.glob(r'*abc*.txt') + glob.glob(r'*123*.txt') + glob.glob(r'*a1b*.txt') for i in list: print
/Users/smcho/Desktop/bracket/[10,20] directory has abc.txt, but when I run this Python code import glob import
import xlwt import xlrd import os.path import os print os.path.abspath(os.curdir) #Create workbook and worksheet
this is my code : import Image,glob files = glob.glob(/small/*.gif) for imageFile in files:
Given the piece of code: from glob import glob, iglob for fn in glob('/*'):
from glob import glob from os.path import isfile def countwords(fp): with open(fp) as fh:
So i have the code: import glob,os import random path = 'C:\\Music\\' aw=[] for
I am trying to print all files with *.txt to a text file, the
import xlrd book = xlrd.open_workbook(File_1.xls) sheet = book.sheet_by_index(0) print sheet.row_values(0)[0] I am trying to
I have this code: # File: zipfile-example-1.py import zipfile,os,glob file = zipfile.ZipFile(Apap.zip, w) #

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.