Skip to content

Instantly share code, notes, and snippets.

@naufraghi
Created May 10, 2011 09:29
Show Gist options
  • Save naufraghi/964166 to your computer and use it in GitHub Desktop.
Save naufraghi/964166 to your computer and use it in GitHub Desktop.
Detect [non] regular files in python
#!/usr/bin/env python
"""
$ echo "ciao" | python is_regular_file.py - <(cat comments.vim) comments.vim
'<stdin>' is not a regular file
'/dev/fd/63' is not a regular file
'comments.vim' is a regular file
"""
import os
import sys
import stat
def is_regular_file(stream):
return stat.S_ISREG(os.fstat(stream.fileno())[stat.ST_MODE])
if len(sys.argv) < 2:
print "Provide file names"
else:
for filename in sys.argv[1:]:
s = open(filename) if filename != "-" else sys.stdin
print "{0!r} is{1} a regular file".format(s.name, "" if is_regular_file(s) else " not")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment