try/exception - error block

Alan Gauld alan.gauld at btinternet.com
Sun Aug 3 16:01:24 EDT 2014


On 03/08/14 18:52, bruce wrote:

>> but in all that.. no one could tell me .. why i'm not getting any
>> errs/exceptions in the err file which gets created on the exception!!!

Does the file actually get created?
Do you see the print statement output - are they what you expect?

Did you try the things Steven suggested.

>>    except Exception, e:
>>      print e
>>      print "pycolFac1 - error!! \n";
>>      name=subprocess.Popen('uuidgen -t', shell=True,stdout=subprocess.PIPE)
>>      name=name.communicate()[0].strip()
>>      name=name.replace("-","_")

This is usually a bad idea. You are using name for the process and its 
output. Use more names...
What about:

uuid=subprocess.Popen('uuidgen -t',shell=True,stdout=subprocess.PIPE)
output=uuid.communicate()[0].strip()
name=output.replace("-","_")

>>      name2="/home/ihubuser/parseErrTest/pp_"+name+".dat"

This would be a good place to insert a print

print name2

>>      ofile1=open(name2,"w+")

Why are you using w+ mode? You are only writing.
Keep life as simple as possible.

>>      ofile1.write(e)

e is quite likely to be empty

>>      ofile1.write(aaa)

Are you sure aaa exists at this point? Remember you are catching all 
errors so if an error happens prior to aaa being created this will
fail.

>>      ofile1.close()

You used the with form earlier, why not here too.
It's considered better style...

Some final comments.
1) You call sys.exit() several times inside
the try block. sys.exit will not be caught by your except block,
is that what you expect?.

2) The combination of confusing naming of variables,
reuse of names and poor code layout and excessive commented
code makes it very difficult to read your code.
That makes it hard to figure out what might be going on.
- Use sensible variable names not a,aaa,z, etc
- use 3 or 4 level indentation not 2
- use a version control system (RCS,CVS, SVN,...) instead
   of commenting out big blocks
- use consistent code style
      eg with f as ... or open(f)/close(f) but not both
- use the os module (and friends) instead of subprocess if possible

3) Have you tried deleting all the files in the
/home/ihubuser/parseErrTest/ folder and starting again,
just to be sure that your current code is actually
producing the empty files?

4) You use tmpParseDir in a couple of places but I don't
see it being set anywhere?


That's about the best I can offer based on the
information available.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos




More information about the Python-list mailing list