daily pastebin goal
35%
SHARE
TWEET

Untitled

a guest Jun 13th, 2018 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import codecs
  2.  
  3.     def countLOC(inFile):
  4.         """ Receives a file and then returns the amount
  5.             of actual lines of code by not counting commented
  6.             or blank lines """
  7.  
  8.         LOC = 0  
  9.         for line in inFile:
  10.             if line.isspace():
  11.                 continue
  12.             comment = line.find('#')
  13.             if comment > 0:
  14.                 for letter in range(comment):
  15.                     if not letter.whitespace:
  16.                         LOC += 1
  17.                         break            
  18.         return LOC
  19.  
  20.     if __name__ == "__main__":
  21.         while True:
  22.             file_loc = input("Enter the file name: ").strip()
  23.             try:
  24.                 source = codecs.open(file_loc)
  25.             except:
  26.                 print ("**Invalid filename**")
  27.             else:
  28.                 break
  29.         LOC_count = countLOC(source)
  30.  
  31.         print ("nThere were {0} lines of code in {1}".format(LOC_count,source.name))
  32.    
  33. File "C:UsersJusten-sanDocumentsEclipse WorkspacecountLOCsrcrootnestedlinesOfCode.py", line 12, in countLOC
  34.         comment = line.find('#')
  35.     TypeError: expected an object with the buffer interface
  36.    
  37. for letter in range(comment):
  38.     if not letter.whitespace:
  39.         LOC += 1
  40.         break
  41.    
  42. def countLOC(inFile):
  43.     LOC = 0  
  44.     for line in inFile:
  45.         if line.isspace():
  46.             continue
  47.         comment = line.find('#')
  48.         if comment > 0:
  49.             for letter in line[:comment]:
  50.                 if not letter.isspace():
  51.                     LOC += 1
  52.                     break
  53.         else:
  54.             LOC += 1
  55.     return LOC
  56.    
  57. def count_LOC(in_file):
  58.     loc = 0  
  59.     for line in in_file:
  60.         line = line.lstrip()
  61.         if len(line) > 0 and not line.startswith('#'):
  62.             loc += 1
  63.     return loc
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top