Skip to content

Instantly share code, notes, and snippets.

@axelpale
Created August 29, 2013 10:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save axelpale/6376313 to your computer and use it in GitHub Desktop.
Save axelpale/6376313 to your computer and use it in GitHub Desktop.
Python script to list files of zero size. Takes in a filepath of a textfile containing a list of filepaths and looks if the list contains files with filesize of zero. The filepaths of files with zero size are stored to file named the original filepath of the list prefixed _zeros
"""
List files of zero size.
Takes in a filepath of a textfile containing a list of filepaths
and looks if the list contains files with filesize of zero.
The filepaths of files with zero size are stored to file named
the original filepath of the list prefixed _zeros
Example Usage
> cat listfile
/home/me/foo_666B.txt
/home/me/bar_420B.txt
/home/me/baz_0B.txt
> python findzeros.py listfile
> cat listfile_zeros
/home/me/baz_0B.txt
Author
Akseli Palen <akseli.palen@gmail.com>
Version
2013-08-29
Used with
python 2.7.3
"""
import os
def findzeros(listfilepath):
zerolist = open(listfilepath + '_zeros', 'a')
with open(listfilepath) as f:
# http://stackoverflow.com/a/12330535/638546
filepaths = f.read().splitlines()
for fp in filepaths:
fsize = os.path.getsize(fp)
if fsize == 0:
zerolist.write(fp + '\n')
zerolist.close()
if __name__ == "__main__":
import sys
abs_filepath = os.path.abspath(sys.argv[1])
findzeros(abs_filepath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment