For python 2.7: pip install glob2

For python 3.7: pip3 install glob2

Answer from Ishan Khatri on Stack Overflow
🌐
Python
docs.python.org › 3 › library › glob.html
glob — Unix style pathname pattern expansion
Source code: Lib/glob.py The glob module finds pathnames using pattern matching rules similar to the Unix shell. No tilde expansion is done, but*,?, and character ranges expressed with[] will be co...
🌐
W3Schools
w3schools.com › python › ref_module_glob.asp
Python glob Module
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Training ... import glob import tempfile import os with tempfile.TemporaryDirectory() as d: open(os.path.join(d, "Emil.txt"), "w").close() open(os.path.join(d, "Linus.txt"), "w").close() open(os.path.join(d, "Tobias.py"), "w").close() hits = glob.glob(os.path.join(d, "*.txt")) print(sorted(os.path.basename(x) for x in hits)) Try it Yourself »
🌐
Built In
builtin.com › software-engineering-perspectives › glob-in-python
Glob in Python Explained | Built In
First, we can use glob to find all files in a directory and its subdirectories that match a search pattern. Then, we read the file as a string and search for the matching search pattern. For example, I know I’ve made a KDE plot before, but I can’t remember where to find it. Let’s find all .ipynb files and search for the string “kdeplot”: import pandas as pd import glob ...
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-use-glob-function-to-find-files-recursively-in-python
How to use Glob() Function to Find Files Recursively in Python - GeeksforGeeks
October 3, 2025 - # Python program to demonstrate # glob using different wildcards import glob print('Named explicitly:') for name in glob.glob('/home/geeks/Desktop/gfg/data.txt'): print(name) # Using '*' pattern print('\nNamed with wildcard *:') for name in glob.glob('/home/geeks/Desktop/gfg/*'): print(name) # Using '?' pattern print('\nNamed with wildcard ?:') for name in glob.glob('/home/geeks/Desktop/gfg/data?.txt'): print(name) # Using [0-9] pattern print('\nNamed with wildcard ranges:') for name in glob.glob('/home/geeks/Desktop/gfg/*[0-9].*'): print(name) Output : We can use the function ·
🌐
Real Python
realpython.com › ref › stdlib › glob
glob | Python Standard Library – Real Python
The Python glob module provides tools to find path names matching specified patterns that follow Unix shell rules. You can use this module for file and directory pattern matching.
🌐
Reddit
reddit.com › r/learnpython › trying to import glob
r/learnpython on Reddit: trying to import glob
August 11, 2026 -

I'm trying to code a little program that will open a CSV file that we should (theoretically) download weekly. The csv filename has the structure "report[numbers].csv" I'd read that you need the glob module so that Python can recognize the wildcard, but I am getting an error when I go the usual python pip -m SomeName route.

Is there an issue with glob, or is there another I can use?

Find elsewhere
🌐
Earth Data Science
earthdatascience.org › home
Use the OS and Glob Python Packages to Manipulate File Paths | Earth Data Science - Earth Lab
November 12, 2020 - Then, you will use the os and glob ... download data from online sources such as the Figshare.com data repository. Begin by importing the necessary packages: os, glob, and earthpy (using the alias et)....
🌐
Server Academy
serveracademy.com › blog › python-glob
Python glob: Find Files with Wildcard Patterns Blog | Server Academy
4 weeks ago - Learn Python glob with tested examples: list files in a directory by pattern, search recursively with , use iglob and root dir, and find old log files.
🌐
GitHub
github.com › python › cpython › blob › main › Lib › glob.py
cpython/Lib/glob.py at main · python/cpython
"""Filename globbing utility.""" · import contextlib · import os · import re · import fnmatch · import functools · import itertools · import operator · import stat · import sys · · · __all__ = ["glob", "iglob", "escape", "translate"] · def glob(pathname, *, root_dir=None, dir_fd=None, recursive=False, include_hidden=False): """Return a list of paths matching a `pathname` pattern.
🌐
YouTube
youtube.com › watch
Python and the Glob Function Easy Tutorial
Enjoy the videos and music you love, upload original content, and share it all with friends, family, and the world on YouTube.
🌐
Python
docs.python.org › id › 3 › library › glob.html
glob --- Unix style pathname pattern expansion — Dokumentasi Python 3.14.5
Kode sumber: Lib/glob.py The glob module finds pathnames using pattern matching rules similar to the Unix shell. No tilde expansion is done, but*,?, and character ranges expressed with[] will be co...
🌐
GitHub
gist.github.com › zeffii › 16a7106fd55650cfe5436de1bbd851c7
glob python files in dir · GitHub
glob python files in dir · Raw · fileglob.py · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters ·
🌐
Python
docs.python.org › es › 3.6 › library › glob.html
11.7. glob — Unix style pathname pattern expansion — documentación de Python - 3.6.15
For example, consider a directory containing the following files: 1.gif, 2.txt, card.gif and a subdirectory sub which contains only the file 3.txt. glob() will produce the following results. Notice how any leading components of the path are preserved. >>> import glob >>> glob.glob('./[0-9].*') ['./1.gif', './2.txt'] >>> glob.glob('*.gif') ['1.gif', 'card.gif'] >>> glob.glob('?.gif') ['1.gif'] >>> glob.glob('**/*.txt', recursive=True) ['2.txt', 'sub/3.txt'] >>> glob.glob('./**/', recursive=True) ['./', './sub/']
Top answer
1 of 3
1

I tried with different Scenarios,and finally got exact solution which satisfies all my conditions. First I'm checking for user input file is available or not in the specified directory, If it is available then globing all files with same file with (_) all at the end appending match file to same list.

If user input if not file is not available in the specified directory then I'm checking for the files with (_) symbol then globing all files into list. At the end iterating through list and got final result.

Program-

from glob import glob
import os
import sys

file_pattern = ''
files_list = list()

arguments = {'ABC', 'PQR', 'XYZ'}

#checking for user provided argument or not
if len(sys.argv[1:2]) is 1:
   file_pattern = str(sys.argv[1:2])
else:   
   print 'run as < python test.py <LineName> >'
   sys.exit(1)
#replace all unnecessary stuff with ('')
file_pattern = file_pattern.replace('[','').replace(']','').replace('\'','')

#checking for line number is provided or not
if file_pattern in arguments:
   print '<Provide LineName with some Number>'
   sys.exit(1)

flag = True
#list of all files containing specified directory
files = os.listdir('<directory name>')

for file_name in files:
   if str(file_name) == str(file_pattern)+'.csv':
      files_list = glob(os.path.join('<directory name>', str(file_pattern)+'_*.csv'))
      #appending match file also to resultant list
      files_list.append('<directory name>'+file_name)
      flag = False
#if specified file is not present in dir check for filename with (_)
if flag:
   files_list = glob(os.path.join('<directory name>', str(file_pattern)+'_*.csv'))

#checking for list contains items or not
if files_list:
   for a_file in sorted(files_list):
      print a_file
else:
   print 'No Such File --> ' + str(file_pattern)+ '\t  <Provide appropriate Name1>'
   sys.exit(1)

Consider directory contains ABC1.csv, ABC1_1.csv, ABC1_2.csv, ABC11.csv, ABC11_1.csv, ABC11_3.csv, ABC11_2.csv files.

Output Scenario :

#if input is ABC1
.\\ABC1.csv
.\\ABC1_1.csv
.\\ABC1_2.csv
#if input is ABC11
.\\ABC11.csv
.\\ABC11_1.csv
.\\ABC11_2.csv
.\\ABC11_3.csv
2 of 3
0

I have a solution. It's not perfect, depends if you have other files in the folder:

file_pattern = 'ABC1'
files_list = glob(os.path.join('<directory name>', str(file_pattern)+'[!0-9]*'))
# output: ABC1.csv, ABC1_1.csv, ABC1_2.csv

file_pattern = 'ABC11'
files_list = glob(os.path.join('<directory name>', str(file_pattern)+'[!0-9]*'))
# output: ['.\\ABC11.csv', '.\\ABC11_1.csv', '.\\ABC11_2.csv', '.\\ABC11_3.csv']

I had the same problem as Jesper. The issue is that although * will match any character, it needs a character!

By selecting any file that doesn't have a digit after the file pattern, we avoid the 1-11 issue.

🌐
Sharp Coder Blog
sharpcoderblog.com › blog › how-to-use-pythons-glob-module-to-find-files-by-pattern
How to Use Python's glob Module to Find Files by Pattern | Sharp Coder Blog
September 4, 2024 - import glob # Find all .txt files in the current directory txt_files = glob.glob('*.txt') print(txt_files)
🌐
Pythonology
pythonology.eu › glob-module-file-searching-in-python
GLOB Module: File Searching in Python – pythonology
Let’s access them using glob and add them to a Pandas dataframe · import pandas as pd import glob # Set path to the folder containing CSV files path = '/path/to/csv/files/*.csv' # Use glob to get a list of all CSV files in the folder files = glob.glob(path) # Initialize an empty DataFrame ...