• Register
0 votes
1.6k views

Problem :

I have attempted to reproduce my problem described below under only Python 2.7.3.

The most reliable way that I found to elicit my problem in the question is to pipe my output of the next test script through the use of  : (under bash):

try:
    for m in range(40):
        print m
except:
    pass

The error message received as below :

% mypython testscript.py | :
close failed in file object destructor:
sys.excepthook is missing
lost sys.stderr

As the test script clearly shows that the error cannot be trapped with the try-except.

My question is as below:

How can I modify my test script above to avoid facing the error message whenmy script is run as shown above (under  the Unix/bash)?

6 5 3
7,540 points

Please log in or register to answer this question.

1 Answer

0 votes

Solution :

When you try to pipe the output of one program into the another, the output produced by the first  writing program gets backed up in the buffer, and it waits for your reading program to request that data from your buffer. As long as your buffer is nonempty, any attempt to made to close your writing file object is supposed to fail with the error. This is the root cause behind the messages you are expearencing.

The specific code that is triggering the error is in your C language implementation of the Python, which explains why you can not catch the error with the try/except block: as it runs after your contents of the script has finished the processing.

9 7 4
38,600 points

Related questions

0 votes
1 answer 19 views
19 views
Problem: Ineed help: Close failed in file object destructor: sys.excepthook is missing lost sys.stderr .
asked Mar 14 Wafa Abu Yousef 6.1k points
1 vote
1 answer 3 views
3 views
Problem : I dont know what’s wrong with the unicorn.rb file. my unicorn.rb config is APP_PATH = "/var/www/demo" working_directory APP_PATH stderr_path APP_PATH + "/log/unicorn.stderr.log" stdout_path APP_PATH + "/log/unicorn.stderr.log" pid APP_PATH + "/tmp/pid/unicorn.pid" running nginx successful. sudo servier nginx start sudo unicorn -c /var/www/demo/config/unicorn.rb -D
asked Apr 16 Sabbir Hossain 14.6k points
0 votes
1 answer 3 views
3 views
Problem: I have a piece of code which is working in Linux, and I am now trying to run it in windows, I import sys but when I use sys.exit(). I get an error, sys is not defined. Here is the begining part of my code try: import numpy as np import ... import leastsq except ImportError: print "Error: missing one of the libraries (numpy, pyfits, scipy, matplotlib)" sys.exit() Why is sys not working??
asked Apr 18 munim01 21k points
0 votes
1 answer 5 views
5 views
Problem: I have a piece of code which is working in Linux, and I am now trying to run it in windows, I import sys but when I use sys.exit(). I get an error, sys is not defined. Here is the begining part of my code try: import numpy as np ... optimize import leastsq except ImportError: print "Error: missing one of the libraries (numpy, pyfits, scipy, matplotlib)" sys.exit() Why is sys not working?
asked Apr 10 Atik03 26.5k points
1 vote
1 answer 3 views
3 views
Problem : Do you have any idea how to fix Exception _mysql_exceptions.OperationalError > (2013, 'Lost connection to MySQL server during query')
asked Apr 26 Abik Dey 4k points
0 votes
1 answer 3 views
3 views
Problem: How to redirect stdout and stderr to file in bash?
asked Mar 30 jamuna1 30.2k points
0 votes
1 answer 11 views
11 views
Problem: can some one help about: bash redirect stderr and stdout to file
asked Mar 4 amir.zaib45 1.2k points
0 votes
1 answer 8 views
8 views
Problem: Please solve it … Because I am unable to find out the solution... import sys for line in sys.stdin: print(line, end="")
asked Mar 31 Ifra 34.7k points
0 votes
0 answers 63 views
63 views
I saw this code to find difference between diagonal sums of a square matrix. #!/bin/python3 import math import os import random import re import sys def diagonalDifference(arr): d1=0 d2=0 a=n-1 print(arr[1][1]) for i in range(0,n): d1+=arr[i][i] ... ['OUTPUT_PATH'], 'w') with fptr = sys.stdout, the output shows no difference. What then is the difference between functions of these two statements?
asked Oct 29, 2020 psandprop 2.4k points
0 votes
1 answer 14 views
14 views
Problem: I want to attach the google-services.json file to my Android Studio project, and every time I try to download it from the developers API from Google, it gives me a json file with a large name that does't correspond to the tutorial's simple "google-services" and ... from there, which seems to be the problem). Where can I safely get this file, and should I rename it or? Thanks in advance.
asked Apr 24 Humaira ahmed 50.7k points