Evaluate coding and style
I recently started playing with Python about 3 days now (Ex Perl guy) and wanted some input on style and structure of what I'm doing before I really start picking up some bad habits here is a simple test tool I wrote to validate home dirs on my system. Please evaluate and let me know what could have been done better. Once again this is really my first time using python. $ ./homedir_exists.py root mqm pcap root successful! Directory: /var/arpwatch not found! pcap successful! mqm successful! $ cat homedir_exists.py #!/usr/bin/env python import sys, os from re import match userlist = [] filename = '/etc/passwd' for user in sys.argv[1:]: userlist.append(user) try: fh = open(filename) except IOError: print No such filename: %s % (filename) def checkDir(username): data = fh.readlines() for line in data: for user in username: if match(user,line): s = line.split(':') if not os.path.isdir(s[5]): print Directory: %s not found! % (s[5]) print s[0] + successful! checkDir(userlist) -- http://mail.python.org/mailman/listinfo/python-list
Random module missing?
I seen some documentation about random.random() but my version seems to be broken? Python 2.3.4 (#1, Jul 16 2009, 07:03:37) [GCC 3.4.6 20060404 (Red Hat 3.4.6-11)] on linux2 Type help, copyright, credits or license for more information. import random dir(random) ['__builtins__', '__doc__', '__file__', '__name__'] random.random() Traceback (most recent call last): File stdin, line 1, in ? AttributeError: 'module' object has no attribute 'rand Please advise thanks. -- http://mail.python.org/mailman/listinfo/python-list
RE: Random module missing?
Thanks Jerry that was the problem. -Original Message- From: python-list-bounces+rodrick.brown=citi@python.org [mailto:python-list-bounces+rodrick.brown=citi@python.org] On Behalf Of Jerry Hill Sent: Wednesday, September 23, 2009 5:11 PM To: python-list@python.org Subject: Re: Random module missing? On Wed, Sep 23, 2009 at 5:00 PM, Brown, Rodrick rodrick.br...@citi.com wrote: I seen some documentation about random.random() but my version seems to be broken? Python 2.3.4 (#1, Jul 16 2009, 07:03:37) [GCC 3.4.6 20060404 (Red Hat 3.4.6-11)] on linux2 Type help, copyright, credits or license for more information. import random dir(random) ['__builtins__', '__doc__', '__file__', '__name__'] random.random() Traceback (most recent call last): File stdin, line 1, in ? AttributeError: 'module' object has no attribute 'rand Please advise thanks. You have a file named random.py (and/or random.pyc) that is shadowing the built in module. Rename it. if you have trouble finding it, you can do the following: import random print random.__file__ -- Jerry -- http://mail.python.org/mailman/listinfo/python-list -- http://mail.python.org/mailman/listinfo/python-list
How can I tell if variable is defined
How could I do the following check in Python In Perl I could do something like if ((defined($a)) { ... } I tried if var is not None: However this doesn't seem to work as described. Thanks. -- http://mail.python.org/mailman/listinfo/python-list