Unix filename wildcards in Python?

馋奶兔 提交于 2019-12-19 03:08:30

问题


How do Unix filename wildcards work from Python?

A given directory contains only subdirectories, in each of which there is (among others) one file whose name ends with a known string, say _ext. The first part of the filename always varies, so I need to get to the file by using this pattern.

I wanted to do this:

directory = "."
listofSubDirs = [x[0] for x in os.walk(directory)]
listofSubDirs = listofSubDirs[1:] #removing "."

for subDirectory in listofSubDirs:
    fileNameToPickle = subDirectory + "/*_ext" #only one such file exists
    fileToPickle = pickle.load(open(fileNameToPickle, "rb"))
    ... do stuff ...

But no pattern matching happens. How does it work under Python?


回答1:


Shell wildcard patterns don't work in Python. Use the fnmatch or glob modules to interpret the wildcards instead. fnmatch interprets wildcards and lets you match strings against them, glob uses fnmatch internally, together with os.listdir() to give you a list of matching filenames.

In this case, I'd use fnmatch.filter():

import os
import fnmatch

for dirpath, dirnames, files in os.walk(directory):
    for filename in fnmatch.filter(files, '*_ext'):
        fileNameToPickle = os.path.join(dirpath, filename)
        fileToPickle = pickle.load(open(fileNameToPickle, "rb"))

If your structure contains only one level of subdirectories, you could also use a glob() pattern that expresses that; the */ in the path of expression is expanded to match all subdirectories:

import glob
import os

for filename in glob.glob(os.path.join(directory, '*/*_ext')):
    # loops over matching filenames in all subdirectories of `directory`.


来源:https://stackoverflow.com/questions/15949694/unix-filename-wildcards-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!