Python Error Bad File Number
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the
Errno 9 Bad File Descriptor Python
company Business Learn more about hiring developers or posting ads with us Stack Overflow bad file descriptor python socket Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2
Python Print Ioerror: [errno 9] Bad File Descriptor
million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up What can lead to “IOError: [Errno 9] Bad file descriptor” during os.system()? up vote 27 down vote favorite 2 ioerror: [errno 9] bad file descriptor print I am using a scientific software including a Python script that is calling os.system() which is used to run another scientific program. While the subprocess is running, Python at some point prints the following: close failed in file object destructor: IOError: [Errno 9] Bad file descriptor I believe that this message is printed at the same time as os.system() returns. My questions now are: Which conditions can lead to this type of IOError? errno 9 socket What does it exactly mean? What does it mean for the subprocess that has been invoked by os.system()? python subprocess posix file-descriptor ioerror share|improve this question edited Apr 24 at 0:10 Aminah Nuraini 2,0921326 asked Oct 7 '11 at 10:54 Jan-Philip Gehrcke 13.6k14173 1 Note: using the subprocess module is preferable over os.system(). –Petr Viktorin Oct 7 '11 at 11:07 :-) I know that and there is a lot of non-perfect stuff in this software. –Jan-Philip Gehrcke Oct 7 '11 at 11:15 add a comment| 2 Answers 2 active oldest votes up vote 22 down vote accepted You get this error message if a Python file was closed from "the outside", i.e. not from the file object's close() method: >>> f = open(".bashrc") >>> os.close(f.fileno()) >>> del f close failed in file object destructor: IOError: [Errno 9] Bad file descriptor The line del f deletes the last reference to the file object, causing its destructor file.__del__ to be called. The internal state of the file object indicates the file is still open since f.close() was never called, so the destructor tries to close the file. The OS subsequently throws an error because of the attempt to close a file that's not open. Since the implementation of os.system() does not create any Python fi
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About
Errno Bad File Number
Us Learn more about Stack Overflow the company Business Learn more about hiring connect:errno=9 developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the
Python Open File For Reading And Writing
Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Getting Errno 9: Bad file http://stackoverflow.com/questions/7686275/what-can-lead-to-ioerror-errno-9-bad-file-descriptor-during-os-system descriptor in python socket up vote 8 down vote favorite 3 My code is this: while 1: # Determine whether the server is up or down try: s.connect((mcip, port)) s.send(magic) data = s.recv(1024) s.close() print data except Exception, e: print e sleep(60) It works fine on the first run, but gives me Errno 9 every time after. What am I doing wrong? BTW, mcip = "mau5ville.com" http://stackoverflow.com/questions/15958026/getting-errno-9-bad-file-descriptor-in-python-socket port = 25565 magic = "\xFE" python sockets share|improve this question asked Apr 11 '13 at 20:13 Mike Savi 881110 1 What do magic, mcip, and port mean? Please post the full code on www.pastebin.com –xxmbabanexx Apr 11 '13 at 20:15 Added them for you –Mike Savi Apr 11 '13 at 20:17 add a comment| 1 Answer 1 active oldest votes up vote 19 down vote accepted You're calling connect on the same socket you closed. You can't do that. As for the docs for close say: All future operations on the socket object will fail. Just move the s = socket.socket() (or whatever you have) into the loop. (Or, if you prefer, use create_connection instead of doing it in two steps, which makes this harder to get wrong, as well as meaning you don't have to guess at IPv4 vs. IPv6, etc.) share|improve this answer answered Apr 11 '13 at 20:18 abarnert 165k11198275 Awesomeee. Thanks! –Mike Savi Apr 11 '13 at 20:20 add a comment| Your Answer draft saved draft discarded Sign up or log in Sign up using Google Sign up using Facebook Sign up using Email a
help? Post your question and get tips & solutions from a community of 418,626 IT Pros & Developers. It's quick & https://bytes.com/topic/python/answers/40073-file-readlines-gives-me-error-bad-file-descriptor easy. file.readlines() - gives me error (bad file descriptor) P: n/a wordsender Hey guys, I can't figure this one out, why is this simple script giving me problems? logfile=file(r'test.txt','w') logfile.write('datetime') test=logfile.readlines() When I run it I get the error message: Traceback (most recent call last): File "C:\Documents and Settings\Gregory\My Documents\Get New Great Job\testfile.py", line 3, in ? test=logfile.readlines() IOError: [Errno 9] Bad bad file file descriptor I'm running Windows XP, Python 2.3.3 (#51, Dec 18 2003, 20:22:39) [MSC v.1200 32 bit (Intel)] on win32 Any help would be greatly appricated. Thanks, Greg Jul 18 '05 #1 Post Reply Share this Question 9 Replies P: n/a Aldo Cortesi Thus spake wo********@gmail.com (wo********@gmail.com): Hey guys, I can't figure this one out, why is this simple script giving bad file descriptor me problems? logfile=file(r'test.txt','w') ^^^ You've opened the file in write mode. To read from the file, you'll have to reopen it in read mode ("r"). Also, if the file you're dealing with really is a log file, you probably want don't want to open it in write mode for writing information either, since that will truncate the file and lose previously logged information. Try append mode ("a") instead. Cheers, Aldo -- Aldo Cortesi al**@nullcube.com http://www.nullcube.com Off: (02) 9283 1131 Mob: 0419 492 863 Jul 18 '05 #2 P: n/a Gurpreet Sachdeva I tried logfile=file(r'test.txt','w+') logfile.write('datetime') test=logfile.readlines() print test I got : Open an encoded file using the given mode and return a wrapped version providing transparent encoding/decoding. Note: The wrapped version will only accept the object format defined by the codecs, i.e. Unicode objects for most builtin codecs. Output is also codec dependent and will usually by Unicode as well. Files are always opened in binary mode, even if no binary mode was specified. This is done to avoid data loss due to encodings using 8-bit values. The default file mode is 'r