Addressing sys.excepthook error in bash script

I wrote a bash script that does exactly what I want, but causing the following error:

close failed in file object destructor: sys.excepthook is missing lost sys.stderr

I am completely fixated on how to solve this problem. Here is the script:

 #!/bin/bash usage () { echo "${0##*/} inputfile outputfile"; exit 1; } (($#==2)) || usage INPUTFILE="$1" OUTPUTFILE="$2" # All that is written between between the 'cat' command and #+ 'EOF' will be sent to the output file. cat <<EOF >$OUTPUTFILE $(date "+Generated on %m/%d/%y at %H:%M:%S") DATA AUDIT: $1 ------------ COLUMN NAMES ------------ $(csvcut -n $INPUTFILE) --------------------------------------- FIRST TEN ROWS OF FIRST FIVE COLUMNS --------------------------------------- $(csvcut -c 1,2,3,4,5 $INPUTFILE | head -n 10) ------------ COLUMN STATS ------------ $(csvcut $INPUTFILE | csvstat ) ---END AUDIT EOF echo "Audited!" 

I am new to shell scripts and very new to python. I would appreciate any help.

+8
python unix bash shell
source share
3 answers

I saw this error when the pipeline was being output from the Python 2.6.2 script to the head command in bash on Ubuntu 9.04. I added try blocks to close stdout and stderr before exiting the script:

 try: sys.stdout.close() except: pass try: sys.stderr.close() except: pass 

I no longer see the error.

+18
source share

You need to do two things:

Step 1:

In the csvcut script, find all the places where sys.stdout.write() is called, make sure sys.stdout.flush() is called after each write() .

Step 2:

After completing step 1, you should now catch an IOError in the Python script. The following is one example of how to handle a broken pipe:

 try: function_with_sys_stdout_write_call() except IOError as e: # one example is broken pipe if e.strerror.lower() == 'broken pipe': exit(0) raise # other real IOError 

Hope this helps!

+3
source share

I assume that the csvcut Python script works the rest, but causes an error when trying to close the files and exit.

If, as you say, the script otherwise works and assumes that the "csvcut" error returns outputs in stderr, then redirecting it to / dev / null will be a temporary fix.

 cat <<EOF >$OUTPUTFILE 2>/dev/null 

Naturally, any other error messages in your heredoc will also be redirected there.

+1
source share

All Articles