Python Io Error Bad File Descriptor
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the bad file descriptor python socket workings and policies of this site About Us Learn more about
Python Print Ioerror: [errno 9] Bad File Descriptor
Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions ioerror: [errno 9] bad file descriptor print Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other.
Errno 9 Bad File Descriptor C
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 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 python subprocess oserror errno 9 bad file descriptor 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? 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
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
Errno Bad File Number
Overflow the company Business Learn more about hiring developers or posting ads with us
Python Open File For Reading And Writing
Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a python file descriptor community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up print: “IOError: [Errno 9] Bad file descriptor” up vote 3 down vote favorite I understand http://stackoverflow.com/questions/7686275/what-can-lead-to-ioerror-errno-9-bad-file-descriptor-during-os-system why I am getting the "Bad file descriptor" error when printing with no console from this post: why am I getting IOError: (9, 'Bad file descriptor') error while making print statements?. My question is, how can I detect if stdout is available? Can I simply do something like this: if os.path.isfile(2): print "text" Thanks python share|improve this question asked Sep 4 '13 at 13:25 Doo Dah 1,41753353 1 You could http://stackoverflow.com/questions/18615130/print-ioerror-errno-9-bad-file-descriptor just wrap a print in a try … except, could you not? –kqr Sep 4 '13 at 13:30 Sure. I had thought of that. I know there are different schools of thought, but, I prefer to be explicit in my actions. That is, look for what except and catch exceptions when I am wrong. –Doo Dah Sep 4 '13 at 14:03 add a comment| 2 Answers 2 active oldest votes up vote 7 down vote accepted os.path.isfile() takes a file path (a string), not a file descriptor (a number), so your solution will not work as you expect. You can use os.isatty() instead: if os.isatty(1): print "text" os.isatty() will return True if its argument is an open file descriptor connected to a terminal. (In passing, note that stdout is file descriptor 1. stderr is file descriptor 2). share|improve this answer answered Sep 4 '13 at 13:31 Frédéric Hamidi 168k22304352 Works great. Thanks. –Doo Dah Sep 4 '13 at 14:02 add a comment| up vote 0 down vote The above answer did not work for me. But perhaps this is a bug in Python 2.x: https://bugs.python.org/issue706263 I am using 2.7. os.isatty(1) returns true always but print still raises an exception after 4k bytes. I am using pythonw.exe to
help? Post your question and get tips & solutions from a community of 418,626 IT Pros & Developers. It's quick & easy. file.readlines() - gives me error (bad file descriptor) https://bytes.com/topic/python/answers/40073-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 file descriptor I'm running Windows XP, Python 2.3.3 (#51, Dec 18 2003, 20:22:39) [MSC v.1200 32 bit bad file (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 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 bad file descriptor 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 'rb' meaning to open the file in binary read mode. encoding specifies the encoding which is to be used for the file. errors may be given to define the error handling. It defaults to 'strict' which causes ValueErrors to be raised in case an encoding and a hell lot of junk. Can anyone