Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is the shortest possible form:</p> <pre><code>for line in open(filename): if line.startswith('#'): continue # PROCESS LINE HERE </code></pre> <p>The <code>startswith()</code> method on a string returns True if the string you call it on starts with the string you passed in.</p> <p>While this is okay in some circumstances like shell scripts, it has two problems. First, it doesn't specify how to open the file. The default mode for opening a file is <code>'r'</code>, which means 'read the file in binary mode'. Since you're expecting a text file it is better to open it with <code>'rt'</code>. Although this distinction is irrelevant on UNIX-like operating systems, it's important on Windows (and on pre-OS X Macs).</p> <p>The second problem is the open file handle. The <code>open()</code> function returns a file object, and it's considered good practice to close files when you're done with them. To do that, call the <code>close()</code> method on the object. Now, Python will <em>probably</em> do this for you, <em>eventually;</em> in Python objects are reference-counted, and when an object's reference count goes to zero it gets freed, and at some point after an object is freed Python will call its destructor (a special method called <code>__del__</code>). Note that I said <em>probably:</em> Python has a bad habit of not actually calling the destructor on objects whose reference count drops to zero shortly before the program finishes. I guess it's in a hurry!</p> <p>For short-lived programs like shell scripts, and particularly for file objects, this doesn't matter. Your operating system will automatically clean up any file handles left open when the program finishes. But if you opened the file, read the contents, then started a long computation without explicitly closing the file handle first, Python is likely to leave the file handle open during your computation. And that's bad practice.</p> <p>This version will work in any 2.x version of Python, and fixes both the problems I discussed above:</p> <pre><code>f = open(file, 'rt') for line in f: if line.startswith('#'): continue # PROCESS LINE HERE f.close() </code></pre> <p>This is the best general form for older versions of Python.</p> <p>As suggested by steveha, using the "with" statement is now considered best practice. If you're using 2.6 or above you should write it this way:</p> <pre><code>with open(filename, 'rt') as f: for line in f: if line.startswith('#'): continue # PROCESS LINE HERE </code></pre> <p>The "with" statement will clean up the file handle for you.</p> <p>In your question you said "lines that start with #", so that's what I've shown you here. If you want to filter out lines that start with <em>optional whitespace</em> and <em>then</em> a '#', you should strip the whitespace before looking for the '#'. In that case, you should change this:</p> <pre><code> if line.startswith('#'): </code></pre> <p>to this:</p> <pre><code> if line.lstrip().startswith('#'): </code></pre> <p>In Python, strings are immutable, so this doesn't change the value of <code>line</code>. The <code>lstrip()</code> method returns a copy of the string with all its leading whitespace removed.</p>
 

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