Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

glob.iglob to find all .txt files in all sub-directories yields error

Simple code gives the below error. It's directly from the docs (https://docs.python.org/3/library/glob.html)

TypeError: iglob() got an unexpected keyword argument 'recursive'

import glob
for filename in glob.iglob('C:\\**\\*txt', recursive=True):
    print filename
like image 649
user2242044 Avatar asked Mar 01 '16 05:03

user2242044


People also ask

How do I use glob to find files recursively?

How to use Glob() function to find files recursively in Python? To use Glob() to find files recursively, you need Python 3.5+. The glob module supports the "**" directive(which is parsed only if you pass recursive flag) which tells python to look recursively in the directories.

What does glob Iglob return?

The glob API iglob(pathname, recursive=False) Same as glob. glob , except that it returns an iterator, meaning that not all values get stored in memory - so can be much more efficient. glob. escape(pathname) Escapes special characters in the passed in “pathname”.

What is glob Iglob?

The glob. iglob() works exactly the same as the glob() method except it returns an iterator yielding file names matching the pattern. This method returns an iterator object that we can iterate over and get the individual file names.

What is glob used for in Python?

glob (short for global) is used to return all file paths that match a specific pattern. We can use glob to search for a specific file pattern, or perhaps more usefully, search for files where the filename matches a certain pattern by using wildcard characters.

How to use GLOB for finding text in files?

glob for finding text in files 1 Use glob to list all files in a directory and its subdirectories that match a file search pattern. 2 Next, read the file and search for the matching text. (You can use regex if you wanted to find a specific pattern in the... More ...

How to use the Glob module in Python to search for files?

Using the glob module we can search for exact file names or even specify part of it using the patterns created using wildcard characters. These patterns are similar to regular expressions but much simpler. We can specify a range of alphanumeric characters inside the []. We need to import Python’s built-in glob module to use the glob () function.

How to remove the files from the directories using Python's Glob () method?

We can remove the files from the directories using the glob () method by iterating over the list and then calling the os.remove () for that file. Both the scandir () and glob () functions are internally searching for the files in a directory that matches a particular pattern. But scandir () is a generator function that returns an iterator object.

How to find files recursively using Python Glob?

For example, if our search path is '/sales/abc.jpeg' and you set recursive to True, it will search abc.jpeg under all subfolders of sales. Use Python 3.5+ to find files recursively using the glob module.


2 Answers

It seems you're using Python 2.7 and reading the Python 3.5 documentation.

like image 74
carlosdc Avatar answered Nov 14 '22 22:11

carlosdc


The recursive parameter has been added in python 3.5, which means version 3.4.3 also has that problem.

If you don't want to upgrade your python version, you can use glob2, which supports recursive calls (**) by default.

like image 45
botchniaque Avatar answered Nov 14 '22 22:11

botchniaque