lc.py

Go to the documentation of this file.
00001 ##
00002 # 
00003 # Counts the number of lines in the input file.  The input file is assumbed
00004 # to be text, but it doesn't have to be.
00005 # 
00006 # Copyright (C) 2002 GDS Software
00007 # 
00008 # This program is free software; you can redistribute it and/or
00009 # modify it under the terms of the GNU General Public License as
00010 # published by the Free Software Foundation; either version 2 of
00011 # the License, or (at your option) any later version.
00012 # 
00013 # This program is distributed in the hope that it will be useful,
00014 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 # GNU General Public License for more details.
00017 # 
00018 # You should have received a copy of the GNU General Public
00019 # License along with this program; if not, write to the Free
00020 # Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00021 # MA  02111-1307  USA
00022 # 
00023 # See http://www.gnu.org/licenses/licenses.html for more details.
00024 # 
00025 
00026 import sys, glob
00027 __version__ = "$Id: lc.py,v 1.3 2002/08/21 12:41:48 donp Exp $"
00028 
00029 def count_lines(files):
00030     retval = 0
00031     for file in sys.argv[1:]:
00032         if not IsFile(file):  continue
00033         try:
00034             fp = open(file, "rb")
00035             lines = fp.readlines()
00036             fp.close()
00037             print "%-8d  %s" % (len(lines), file)
00038         except:
00039             sys.stderr.write("Couldn't read \"%s\"\n" % file)
00040             retval = 1
00041     return retval
00042 
00043 ##
00044 # Return 1 if file_name is a file; otherwise return 0.
00045 #     
00046 def IsFile(file_name):
00047     import os
00048     try:
00049         s = os.stat(file_name)
00050         return ((0100000 & s[0]) == 0100000)
00051     except:
00052         return 0
00053 
00054 if __name__ == "__main__":
00055     if len(sys.argv) < 2:
00056         print "Usage:  lc file1 [file2...]"
00057         sys.exit(1)
00058     sys.exit(count_lines(sys.argv[1:]))
00059 
00060 
00061 

© Copyright 2008-2009 Vyper Logix Corp., All Right Reserved; If you reference this document or any part of this document you must use the citation verbatim (including the link) "© Copyright 2008-2009 Vyper Logix Corp., All Right Reserved."

Notice: This source code contained in this document is NOT open source and is NOT being distributed as open source.

122,241 lines of code and growing...