os.walk

1
2
3
4
5
import os

for root, dirs, files in os.walk('.'):
for file in files:
print file

查找指定文件

glob.glob

1
2
3
4
import glob

for file in glob.glob('*.txt'):
print(file)

fnmatch.fnmatch

1
2
3
4
5
6
import fnmatch
import os

for file in os.listdir('.'):
if fnmatch.fnmatch(file, '*.txt'):
print file

re.match

1
2
3
for d in [f for f in os.listdir('.') if re.match(r'build-\(.*\)-.*', f)]:
print('cleanning {} ...'.format(d))
shutil.rmtree(d)

参考

  1. http://stackoverflow.com/questions/2225564/get-a-filtered-list-of-files-in-a-directory

留言