如何使用os.listdir()忽略隐藏文件?
My python script executes an os.listdir(path)
where the path is a queue containing archives that I need to treat one by one.
我的python脚本执行一个os.listdir(路径),其中路径是一个包含归档的队列,我需要逐一处理这些归档。
The problem is that I'm getting the list in an array and then I just do a simple array.pop(0)
. It was working fine until I put the project in subversion. Now I get the .svn
folder in my array and of course it makes my application crash.
问题是,我在数组中获取列表,然后做一个简单的array.pop(0)在我把这个项目放到subversion中之前,它一直运行得很好。现在我在数组中得到了。svn文件夹,当然这会导致应用程序崩溃。
So here is my question: is there an existing function that ignore hidden files when executing an os.listdir()
and if not what would be the best way?
因此,我的问题是:是否存在在执行os.listdir()时忽略隐藏文件的现有函数,如果没有,最好的方法是什么?
Thank you.
谢谢你!
7 个解决方案
#1
65
You can write one yourself:
你可以自己写一个:
def listdir_nohidden(path): for f in os.listdir(path): if not f.startswith('.'): yield f
Or you can use a glob:
或者你也可以使用glob:
def listdir_nohidden(path): return glob.glob(os.path.join(path, '*'))
Either of these will ignore all filenames beginning with '.'
.
其中任何一个都将忽略从“.”开始的所有文件名。
更多相关文章
- Gunicorn + Django + Heroku。Python的路径问题
- py2exe使用相对路径的当前目录问题
- tensorflow 变量定义路径//问题
- python在windows中的文件路径问题
- Python添加默认模块搜索包路径
- 《数据结构与算法Python语言描述》裘宗燕 笔记 第五章 栈和队列
- 如何获得正在运行的Python脚本的路径?(复制)
- 多队列网卡CPU中断均衡
- 如何在Linux中以编程方式获取给定相对路径的绝对路径?