开发者

Finding empty files in Python

开发者 https://www.devze.com 2023-04-01 03:13 出处:网络
What\'s the best way in Python to find all empty (zero byte) files, recursively, from a given directory?I could use os.walk and stat each fi开发者_Go百科le, but that seems very inefficient.Thanks.I\'m

What's the best way in Python to find all empty (zero byte) files, recursively, from a given directory? I could use os.walk and stat each fi开发者_Go百科le, but that seems very inefficient. Thanks.


I'm not sure how you'd get more efficient than os.walk and stat since that's fundamentally what you're doing. You could use some external command/service through Python's subprocess.Popen but then that's hardly "in python".


First, you should make it work, then figure out how to make it faster. You shouldn't start with a feeling that the most obvious way "seems very inefficient" If you want efficient, you can try the native OS.

os.system("find . -empty")
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号