Python 3.5 glob.glob() 2nd param (*) and how to detect files/folders beginning with "."?

Peter Otten __peter__ at web.de
Tue Jul 26 12:48:52 EDT 2016


Malcolm Greene wrote:

> 2. Is there a technique for using glob.glob() to recognize files and
>    folders that begin with a period, eg. ".profile"? The documentation
>    states: "If the directory contains files starting with . they won’t
>    be matched by default.". Any suggestions on what the non-default
>    approach is to match these type of files?

I don't think there is a clean way. You can monkey-patch if you don't need 
the default behaviour elsewhere in your application:


$ touch foo .bar baz
$ python3
Python 3.4.3 (default, Oct 14 2015, 20:28:29) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import glob
>>> glob.glob("*")
['foo', 'baz']
>>> glob._ishidden = lambda path: False
>>> glob.glob("*")
['.bar', 'foo', 'baz']





More information about the Python-list mailing list