1

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 file, but that seems very inefficient. Thanks.

1
  • 4
    How are you going to know if they're zero-byte unless you stat them? Commented Sep 1, 2011 at 17:20

2 Answers 2

2

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".

Sign up to request clarification or add additional context in comments.

1 Comment

After doing some testing with os.walk and stat, it doesn't seem to be much different than the OS command "find <dir> -empty". Given my minimal expertise, I was thinking there may be a better way, but apparently not. Thanks for the quick response.
0

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")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.