Addressing sys.excepthook error in bash script

前端 未结 3 1331
暗喜
暗喜 2021-01-12 06:46

I\'ve written a bash script that is doing exactly what I want it to do, but kicking out the following error:

close failed in file object destructor: sys.except

相关标签:
3条回答
  • 2021-01-12 07:26

    I was seeing this error when piping output from a Python 2.6.2 script into 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 am no longer seeing the error.

    0 讨论(0)
  • 2021-01-12 07:35

    There are two steps you need to perform:

    Step 1:

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

    Step 2:

    With step 1 completed you should now be able to capture IOError within the Python script. Below is one example on how to handle 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 it helps!

    0 讨论(0)
  • 2021-01-12 07:36

    I assume that the csvcut Python script is otherwise functional but is throwing up an error when it tries to close files and exit.

    If, as you say, the script is otherwise working and assuming that the error 'csvcut' is throwing up outputs to stderr then redirecting it to /dev/null would be a temp fix.

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

    Naturally any other error messages in your heredoc will also redirect there.

    0 讨论(0)
提交回复
热议问题