擅长:python、mysql、java
<p>在<a href="http://docs.python.org/library/pwd.html#pwd.getpwnam" rel="nofollow">the same page of the manual</a>:</p>
<blockquote>
<pre><code>pwd.getpwnam(name)
</code></pre>
<p>Return the password database entry for the given user name.</p>
</blockquote>
<p>这是现有用户和不存在用户的结果:</p>
<pre><code>>>> import pwd
>>> pwd.getpwnam('root')
pwd.struct_passwd(pw_name='root', pw_passwd='*', pw_uid=0, pw_gid=0, pw_gecos='System Administrator', pw_dir='/var/root', pw_shell='/bin/sh')
>>> pwd.getpwnam('invaliduser')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'getpwnam(): name not found: invaliduser'
</code></pre>
<p>因此,您可以:</p>
^{pr2}$
<p>注意:<code>in</code>运算符无论如何都会执行一个循环来检查给定的项是否在列表(<a href="http://pyref.infogami.com/in" rel="nofollow">ref</a>)中。在</p>