The glob.glob gained the root_dir and dir_fd parameters in Python 3.10.
See Package description
You could do something like this :
def search_xlsx(rootdir):
file_list = []
for root, directories, file in os.walk(rootdir):
for file in file:
if(file.endswith(".xlsx")):
file_list.append(file)
return file_list
Answer from gcher495 on Stack Overflowpython 3.x - Error: glob() got an unexpected keyword argument 'root_dir' - Stack Overflow
python - How to use glob() to find files recursively? - Stack Overflow
linux - Python glob, os, relative path, making filenames into a list - Stack Overflow
Add the root_dir and dir_fd parameters in glob.glob()
There are a couple of ways:
pathlib.Path().rglob()
Use pathlib.Path().rglob() from the pathlib module, which was introduced in Python 3.5.
from pathlib import Path
for path in Path('src').rglob('*.c'):
print(path.name)
glob.glob()
If you don't want to use pathlib, use glob.glob():
from glob import glob
for filename in glob('src/**/*.c', recursive=True):
print(filename)
For cases where matching files beginning with a dot (.); like files in the current directory or hidden files on Unix based system, use the os.walk() solution below.
os.walk()
For older Python versions, use os.walk() to recursively walk a directory and fnmatch.filter() to match against a simple expression:
import fnmatch
import os
matches = []
for root, dirnames, filenames in os.walk('src'):
for filename in fnmatch.filter(filenames, '*.c'):
matches.append(os.path.join(root, filename))
This version should also be faster depending on how many files you have, as the pathlib module has a bit of overhead over os.walk().
For python >= 3.5 you can use **, recursive=True, i.e.:
import glob
for f in glob.glob('/path/**/*.c', recursive=True):
print(f)
If recursive is
True(defaultFalse), the pattern**will match any files and zero or moredirectoriesandsubdirectories. If the pattern is followed by anos.sep, only directories andsubdirectoriesmatch.
Python 3 Demo
glob will return paths in a format matching your query, so that
glob.glob("/home/usr/dir/*.root")
# ['home/usr/dir/foo.root', 'home/usr/dir/bar.root', ...]
glob.glob("*.root")
# ['foo.root', 'bar.root', ...]
glob.glob("./*.root")
# ['./foo.root', './bar.root', ...]
...and so forth.
To get only the filename, you can use path.basename of the os module, something like this:
from glob import glob
from os import path
pattern = "/home/usr/dir/*.root"
files = [path.basename(x) for x in glob(pattern)]
# ['foo.root', 'bar.root', ...]
...or, if you want to prepend the dir part:
pattern = "/home/usr/dir/*.root"
files = [path.join('dir', path.basename(x)) for x in glob(pattern)]
# ['dir/foo.root', 'dir/bar.root', ...]
...or, if you really want the path separator at the start:
from glob import glob
import os
pattern = "/home/usr/dir/*.root"
files = [os.sep + os.path.join('dir', os.path.basename(x)) for x in glob(pattern)]
# ['/dir/foo.root', '/dir/bar.root', ...]
Using path.join and path.sep will make sure that the correct path syntax is used, depending on your OS (i.e. / or \ as a separator).
Depending on what you are really trying to do here, you might want to look at os.path.relpath, for the relative path. The title of your question indicates that relative paths might be what you are actually after:
pattern = "/home/usr/dir/*.root"
files = [os.path.relpath(x) for x in glob(pattern)]
# files will now contain the relative path to each file, from the current working directory
just use glob for getting the list you want
and then use os.path.relpath on each file
import glob
files_names = []
for file in glob.glob('/home/usr/dir/*.root'):
files_names.append(os.path.relpath(file, "/home/usr"))
You can also use regex
import re
files_names.append(re.sub(r'//home//usr//','', file, flags=re.I))
In Python 3.5 and newer use the new recursive **/ functionality:
configfiles = glob.glob('C:/Users/sam/Desktop/file1/**/*.txt', recursive=True)
When recursive is set, ** followed by a path separator matches 0 or more subdirectories.
In earlier Python versions, glob.glob() cannot list files in subdirectories recursively.
In that case I'd use os.walk() combined with fnmatch.filter() instead:
import os
import fnmatch
path = 'C:/Users/sam/Desktop/file1'
configfiles = [os.path.join(dirpath, f)
for dirpath, dirnames, files in os.walk(path)
for f in fnmatch.filter(files, '*.txt')]
This'll walk your directories recursively and return all absolute pathnames to matching .txt files. In this specific case the fnmatch.filter() may be overkill, you could also use a .endswith() test:
import os
path = 'C:/Users/sam/Desktop/file1'
configfiles = [os.path.join(dirpath, f)
for dirpath, dirnames, files in os.walk(path)
for f in files if f.endswith('.txt')]
There's a lot of confusion on this topic. Let me see if I can clarify it (Python 3.7):
glob.glob('*.txt') :matches all files ending in '.txt' in current directoryglob.glob('*/*.txt') :same as 1glob.glob('**/*.txt') :matches all files ending in '.txt' in the immediate subdirectories only, but not in the current directoryglob.glob('*.txt',recursive=True) :same as 1glob.glob('*/*.txt',recursive=True) :same as 3glob.glob('**/*.txt',recursive=True):matches all files ending in '.txt' in the current directory and in all subdirectories
So it's best to always specify recursive=True.
i'm trying to find files in my directory but when i write:
glob.glob('*')
it goes to .vscode folder and finds files over there.
Hello,
Having issues working in VS Code (python 3.13.3) using "glob" to search for a list of .csv files in a folder. Not sure what set the error could be referring to, or how the module indexes (or doesn't I guess). Any help much appreciated.
Example code and terminal output down below:
import pandas as pd
import glob
import plotly.graph_objects as go
z_ref = 92.5
tol = 0.07
z_usl = z_ref * (1+tol)
z_lsl = z_ref * (1-tol)
folder = {f".\downloads*.csv"}
lst_csvs = glob.glob(folder)
print(lst_csvs)> & C:/Users/Frameboiii/AppData/Local/Microsoft/WindowsApps/python3.13.exe c:/Users/Frameboiii/Downloads/random/script.py
c:\Users\Frameboiii\Downloads\random\script.py:9: SyntaxWarning: invalid escape sequence '\d'
folder = {f".\downloads*.csv"}
Traceback (most recent call last):
File "c:\Users\Frameboiii\Downloads\random\script.py", line 10, in <module>
lst_csvs = glob.glob(folder)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.13_3.13.1008.0_x64__qbz5n2kfra8p0\Lib\glob.py", line 31, in glob
return list(iglob(pathname, root_dir=root_dir, dir_fd=dir_fd, recursive=recursive,
include_hidden=include_hidden))
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.13_3.13.1008.0_x64__qbz5n2kfra8p0\Lib\glob.py", line 51, in iglob
root_dir = pathname[:0]
TypeError: 'set' object is not subscriptable