python-3.x - if condition inside lambda while using posix path
问题描述
So, I'm trying to remove hidden files from POSIX PATH i.e.
[PosixPath('/Users/abhimanyuaryan/../imagenette-160/.DS_Store'),
PosixPath('/Users/abhimanyuaryan/../imagenette-160/train'),
PosixPath('/Users/abhimanyuaryan/../imagenette-160/val')]
This .DS_Store
doesn't make any sense here.
p = Path('/Users/abhimanyuaryan/../imagenette-160/')
I wrote this code. Tried adding if condition at back but I'm confused how do I do that. For a fact I know this code is wrong. Because even if it were a string. .DS_Store
is not x[0]
It has to be after last \
. I think I might need some regex here to find \.
& then exclude it from POSIX PATH
but first I need to correct my if statement which doesn't seem to be correct
Path.ls = lambda x: list(x.iterdir()) if str(x)[0] != '.'
I also saw few labda and if usage questions on SO but didn't understand the correct syntax
解决方案
你还需要有一个else
:
Path.ls = lambda x: list(x.iterdir()) if str(x)[0] != '.' else None
我用None
例如。
推荐阅读
- c# - 从用户上下文应用程序(C#、控制台应用程序)访问系统应用程序数据
- java - 如何为 Wear Os 发出通知?
- ios - performSegue 视图不在窗口层次结构中
- java - LocalDate.parse 是否默默地纠正了天数?
- python - 使用 Python 后台脚本捕获屏幕截图并保存到文档
- networking - Openshift/Kubernates kube dns 最佳实践 (ndots = 5)
- sql - Pivot, CONCAT, 一张表和 INNER JOIN 与另一张表创建单表
- python - 使用代码自定义 Jupyter Notebook 单元格行为
- python - 如何从 url 获取文件缺少的内容长度?
- javascript - 如何使在线会员柜台即时更新?