find function

U

Uwe Mayer

Hi,

is there for python a find() function, like the perl module Find or the
command line tool find?

All I found was os.listdir(<path>), but that does return all files in a
folder. I'd like to have i.e. regex or shell-like file selections, as in:

$ find -name \*.pyc -maxdepth 1

Thanks
Ciao
Uwe
--
 
M

Mark McEahern

Hi,

is there for python a find() function, like the perl module Find or the
command line tool find?

Try:

import glob
pattern = '*.pyc'
for name in glob.glob(pattern):
print name

glob's not recursive, though. For that, consider os.walk:

import os
path = '.'
extensions = ('.pyc',)
for root, dirs, files in os.walk(path):
for name in files:
prefix, ext = os.path.splitext(name)
if ext not in extensions:
continue
fullname = os.path.join(root, name)
print fullname

The above code may have syntax errors since I didn't run it.

// m
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,176
Messages
2,570,950
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top