将通配符与os.path.isfile()一起使用 [英] Use wildcard with os.path.isfile()

查看:101
本文介绍了将通配符与os.path.isfile()一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查目录中是否有任何.rar文件.它不需要递归.

I'd like to check if there are any .rar files in a directory. it doesn’t need to be recursive.

使用通配符os.path.isfile()是我最好的猜测,但这是行不通的.那我该怎么办?

Using wildcard with os.path.isfile() was my best guess, but it doesn't work. What can I do then?

谢谢.

推荐答案

glob 是您需要什么.

>>> import glob
>>> glob.glob('*.rar')   # all rar files within the directory, in this case the current working one

如果路径是现有的常规文件,则

os.path.isfile()返回True.因此,它用于检查文件是否已经存在并且不支持通配符. glob做到了.

os.path.isfile() returns True if a path is an existing regular file. So that is used for checking whether a file already exists and doesn't support wildcards. glob does.

这篇关于将通配符与os.path.isfile()一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆