Python:如何捕获os.chdir FileNotFoundError执行

时间:2019-01-29 02:07:38

标签: python-3.x

我在下面有以下代码:(上午使用python 3.6.4)

def recursedir(dirname, filename):
        os.chdir(dirname)
        try:
                # print('Searching in ' + os.getcwd())
                for root, subdir, files in os.walk('.'):
                        for fname in files:
                                if fnmatch.fnmatch(fname, filename):
                                        name = os.path.join(root, fname)
                                        name = re.sub(r"\.\\","", name)
                                        return name
        except FileNotFoundError:
                print(dirname + ' does NOT exist. SKIPPED')
        return None

但是似乎“ except FileNotFoundError”无法捕获该错误,但我仍然收到此错误消息:

  

FileNotFoundError:[WinError 2]系统找不到文件   指定:“ e:\ workspace \ Share”

e:\ workspace \ Share确实不存在,但是我只想打印一条消息,说“此目录不存在”并继续。

0 个答案:

没有答案