Created
May 10, 2011 09:29
-
-
Save naufraghi/964166 to your computer and use it in GitHub Desktop.
Detect [non] regular files in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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