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

Discussion in 'Python' started by wordsender@gmail.com, Jan 6, 2005.

  1. Guest

    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
     
    , Jan 6, 2005
    #1
    1. Advertising

  2. Aldo Cortesi Guest

    Thus spake ():

    > 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 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

    http://www.nullcube.com
    Off: (02) 9283 1131
    Mob: 0419 492 863
     
    Aldo Cortesi, Jan 6, 2005
    #2
    1. Advertising

  3. Re: file.readlines() - gives me error (bad file descriptor)

    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 <> 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=
    > [mailto:python-list-bounces+vishnube=] On Behalf Of
    >
    > Sent: Thursday, January 06, 2005 11:53 AM
    > To:
    > 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
     
    Gurpreet Sachdeva, Jan 6, 2005
    #3
  4. Re: file.readlines() - gives me error (bad file descriptor)

    logfile=file(r'test.txt','a+')
    logfile.write('datetime')
    logfile.flush()
    test=logfile.readlines()
    print test

    I added logfile.flush(), the 'datetime' was written in the file
    correctly but I couldn't get any result...

    Crazy!
    Garry
     
    Gurpreet Sachdeva, Jan 6, 2005
    #4
  5. Binu K S Guest

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

    There's nothing crazy going on here. The file's current position moves
    as you write into it. Both read and write operation use the same
    offset. The tell() method gives you the current position at any time.
    When you append to a file the position moves to the end of the file so
    that the next write happens at the end. You normally wouldn't read
    from the file that you are writing into. To read from the beginning,
    change the position with seek. Here's how you do it:

    >>> logfile=file(r'test.txt','a+')
    >>> logfile.write('datetime')
    >>> logfile.flush()
    >>> test=logfile.readlines()
    >>> print test

    []
    >>> logfile.tell()

    8L
    >>> logfile.seek(0,0)
    >>> test=logfile.readlines()
    >>> print test

    ['datetime']


    On Thu, 6 Jan 2005 12:30:03 +0530, Gurpreet Sachdeva
    <> wrote:
    > logfile=file(r'test.txt','a+')
    > logfile.write('datetime')
    > logfile.flush()
    > test=logfile.readlines()
    > print test
    >
    > I added logfile.flush(), the 'datetime' was written in the file
    > correctly but I couldn't get any result...
    >
    > Crazy!
    > Garry
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
     
    Binu K S, Jan 6, 2005
    #5
  6. Re: file.readlines() - gives me error (bad file descriptor)

    On Thu, 6 Jan 2005 12:55:22 +0530, Binu K S <> wrote:
    >>>The file's current position moves as you write into it.

    I concure and have figured out the solution BUT while reading from the
    file from the position where the file handler is, should return
    "Null/Blank/Anything in this world" BUT should not write into the
    file...
    The information written was that in the Buffer of the handler... Does
    that mean reading a file will actually release the buffer by throwing
    the contents in the file???

    Please Comment...

    >>>You normally wouldn't read from the file that you are writing into.


    May be applicable in handling logs...

    Regards,
    Garry
     
    Gurpreet Sachdeva, Jan 6, 2005
    #6
  7. Binu K S Guest

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

    On Thu, 6 Jan 2005 13:17:11 +0530, Gurpreet Sachdeva
    <> wrote:
    > On Thu, 6 Jan 2005 12:55:22 +0530, Binu K S <> wrote:
    > >>>The file's current position moves as you write into it.

    > I concure and have figured out the solution BUT while reading from the
    > file from the position where the file handler is, should return
    > "Null/Blank/Anything in this world" BUT should not write into the
    > file...
    > The information written was that in the Buffer of the handler... Does
    > that mean reading a file will actually release the buffer by throwing
    > the contents in the file???
    >


    I'm sorry, I didn't get what you trying to say here. Where do you see
    a read altering the file? An example might help.

    > Please Comment...
    >
    > >>>You normally wouldn't read from the file that you are writing into.

    >
    > May be applicable in handling logs...


    Sometimes after opening a file you read the contents of a file and
    then append to it. I haven't seen writing to a file and then going
    back to see what was written.

    >
    > Regards,
    > Garry
    >
     
    Binu K S, Jan 6, 2005
    #7
  8. Re: file.readlines() - gives me error (bad file descriptor)

    On Thu, 6 Jan 2005 14:27:40 +0530, Binu K S <> wrote:
    > I'm sorry, I didn't get what you trying to say here. Where do you see
    > a read altering the file? An example might help.


    Please try:

    logfile=file(r'test.txt','w+')
    logfile.write('datetime')

    Check the contents of test.txt, you will get what ever was required...

    Now Run:

    logfile=file(r'test.txt','w+')
    logfile.write('datetime')
    logfile.readlines()

    and check the contents of test.txt???
    My doubt is that logfile.write('datetime') will write datetime that is
    universaly accepted but why all that junk is appending to that when I
    add logfile.readlines()

    Strange or Am I on drugs??? :eek:)

    Please Comment...

    Garry
     
    Gurpreet Sachdeva, Jan 6, 2005
    #8
  9. Binu K S Guest

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

    http://mail.python.org/pipermail/python-bugs-list/2001-October/007650.html
    Rest assured you're not on drugs :)

    On Thu, 6 Jan 2005 14:45:24 +0530, Gurpreet Sachdeva
    <> wrote:
    > On Thu, 6 Jan 2005 14:27:40 +0530, Binu K S <> wrote:
    > > I'm sorry, I didn't get what you trying to say here. Where do you see
    > > a read altering the file? An example might help.

    >
    > Please try:
    >
    > logfile=file(r'test.txt','w+')
    > logfile.write('datetime')
    >
    > Check the contents of test.txt, you will get what ever was required...
    >
    > Now Run:
    >
    > logfile=file(r'test.txt','w+')
    > logfile.write('datetime')
    > logfile.readlines()
    >
    > and check the contents of test.txt???
    > My doubt is that logfile.write('datetime') will write datetime that is
    > universaly accepted but why all that junk is appending to that when I
    > add logfile.readlines()
    >
    > Strange or Am I on drugs??? :eek:)
    >
    > Please Comment...
    >
    > Garry
    >
     
    Binu K S, Jan 6, 2005
    #9
  10. wrote:
    > 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
    >


    As logfile is opened in write mode you cannot read it. You've to open it
    in read mode for reading.


    Greetings,
    Abhijit

    --
    Everybody wants to go to heaven but nobody wants to die
     
    Abhijit Soman, Jan 6, 2005
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Kevin
    Replies:
    4
    Views:
    1,063
    David Bolen
    Jul 7, 2003
  2. drs
    Replies:
    0
    Views:
    381
  3. Vishnu
    Replies:
    0
    Views:
    512
    Vishnu
    Jan 6, 2005
  4. lynology

    Bad File Descriptor Error on strcat/strcpy

    lynology, Aug 18, 2004, in forum: C Programming
    Replies:
    4
    Views:
    506
    Chris Torek
    Aug 18, 2004
  5. nik
    Replies:
    0
    Views:
    653
Loading...

Share This Page