Breaking the barrier of a broken paradigm... part 1
john.jython at gmail.com
Tue Mar 25 12:04:37 CET 2008
On Mar 25, 6:34 am, Bryan Olson <fakeaddr... at nowhere.org> wrote:
> john s. wrote:
> > #/usr/bin/enviro python
> > #Purpose - a dropped in useracct/pass file is on a new server to build
> > #a cluster... Alas there are no home directories.. Let me rip through
> > #the passfile, grab the correct field (or build it) and use it to make
> > #the directory!
> > import os, sys, string, copy, getopt, linecache
> > from traceback import format_exception
> > #The file we read in...
> > fileHandle = "/etc/passwd"
> > srcFile = open(fileHandle,'r')
> > srcList = srcFile.readlines()
> > #yah, a for loop that "iterates" through the file of "lines"
> > for i in srcList:
> Convention is that the name i is for an integer.
just a bit of silly walk on my part in the comment field...
> > strUsr = string.split(i,":")
> > theUsr = strUsr
> > usrHome = "/expirt/home/",theUsr,"/"
> > usrHome = ''.join(usrHome)
> As Ryan noted, os.path is the favored way.
> > print "printing usrHome:",usrHome
> > print "is it a dir?: ", os.path.isdir(usrHome)
> > #try to test if it's a dir... for some reason this mis-behaves...
> > #maybe I'm not thinking about it correctly..
> > if os.path.isdir(usrHome) != 'False':
> That should always evaluate true. False != 'False'. I think you want:
ok... yes I do
> if not os.path.exists(usrHome):
> > print "User Home dir doesn't exist creating."
> > try:
> > os.makedirs(usrHome)
> > os.system('/usr/bin/chmod 750 ' + usrHome)
> > os.system('/usr/bin/chown ' + theUsr + ' ' + usrHome)
> > except Exception, e:
> > print e
> I don't think you want to catch the exception there. If creating the dir
> fails, the next bits of code should not execute.
ok. I see that now.
> > #OMG, there is directory that happens to already exist! well,
> > #love for the queen dictates we provide "due
> > #dilligence", (e.g. wipe our asses properly)
> The path could exist but not be a directory.
So isadir is slightly better then exists?
It felt like it was not working right...
(probably because I was double testing the exp I see now)
> > else:
> > print "User Home dir exist, checking and fixing permissions."
> > sys.exit("Thanks for using pyDirFixr...")
> Given the Unixy nature of your code, you probably want to sys.exit(0)
> for success and 1 or 2 for failure.
got it. I'm guessing I'm better off with a last line print "g'bye"
then sys.exit versus trying both at once...
> Happy hacking,
Thanks Bryan :)
More information about the Python-list