Note that there are some explanatory texts on larger screens.

plurals
  1. POstr.startswith() not working as I intended
    text
    copied!<p>I'm trying to test for a /t or a space character and I can't understand why this bit of code won't work. What I am doing is reading in a file, counting the loc for the file, and then recording the names of each function present within the file along with their individual lines of code. The bit of code below is where I attempt to count the loc for the functions.</p> <pre><code>import re ... else: loc += 1 for line in infile: line_t = line.lstrip() if len(line_t) &gt; 0 \ and not line_t.startswith('#') \ and not line_t.startswith('"""'): if not line.startswith('\s'): print ('line = ' + repr(line)) loc += 1 return (loc, name) else: loc += 1 elif line_t.startswith('"""'): while True: if line_t.rstrip().endswith('"""'): break line_t = infile.readline().rstrip() return(loc,name) </code></pre> <p>Output:</p> <pre><code>Enter the file name: test.txt line = '\tloc = 0\n' There were 19 lines of code in "test.txt" Function names: count_loc -- 2 lines of code </code></pre> <p>As you can see, my test print for the line shows a /t, but the if statement explicitly says (or so I thought) that it should only execute with no whitespace characters present.</p> <p>Here is my full test file I have been using:</p> <pre><code>def count_loc(infile): """ Receives a file and then returns the amount of actual lines of code by not counting commented or blank lines """ loc = 0 for line in infile: line = line.strip() if len(line) &gt; 0 \ and not line.startswith('//') \ and not line.startswith('/*'): loc += 1 func_loc, func_name = checkForFunction(line); elif line.startswith('/*'): while True: if line.endswith('*/'): break line = infile.readline().rstrip() return loc if __name__ == "__main__": print ("Hi") Function LOC = 15 File LOC = 19 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload