file.readlines() - gives me error (bad file descriptor)

Gurpreet Sachdeva gurpreet.sachdeva at gmail.com
Thu Jan 6 01:53:41 EST 2005


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 explain that???
AFAIK w+ was ment for opening a file for both writting and reading...

Regards,
Garry


On Thu, 6 Jan 2005 12:03:27 +0530, Vishnu <vishnube at acmet.com> wrote:
> logfile = file(r'test.txt','w')
> logfile.write('datetime')
> logfile.close()                         # <- close the file
> 
> logfile = file(r'test.txt','r') # <- Open the file in read mode
> test=logfile.readlines()
> 
> 
> ~Vishnu.
> 
> -----Original Message-----
> From: python-list-bounces+vishnube=acmet.com at python.org
> [mailto:python-list-bounces+vishnube=acmet.com at python.org] On Behalf Of
> wordsender at gmail.com
> Sent: Thursday, January 06, 2005 11:53 AM
> To: python-list at python.org
> Subject: file.readlines() - gives me error (bad file descriptor)
> 
> 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 (Intel)] on win32
> Any help would be greatly appricated.
> 
> Thanks,
> 
> Greg
> 
> --
> http://mail.python.org/mailman/listinfo/python-list
> 
> --
> http://mail.python.org/mailman/listinfo/python-list
> 


-- 
Thanks and Regards,
GSS



More information about the Python-list mailing list