Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete empty files

Tags:

python

I'd like to remove files which is empty.

import os
book_list = ['Automate the Boring Stuff with Python.pdf',
 'OReilly.Think.Python.2nd.Edition.2015.12.pdf',
 'Apress-Magnus_Lie_Hetland-python_algorithms.pdf',
 'Python for Data Analysis - 2012.pdf']
for book in book_list:
    if os.path.getsize(book) == 0:
        os.remove(book)

It seems not pythonic because of the boolean comparsion ==.

How to accomplish such a task in an alternative way?


1 Answers

That is correct usage of the equality operator. If you want to be 'fancy' you can use the code provided by sam-pyt:

if not os.path.getsize(book)
like image 71
bicelot3 Avatar answered Sep 20 '25 01:09

bicelot3