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 9 29184
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 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
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 <vi******@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: py************************************@python.org [mailto:py************************************@pyth on.org] On Behalf Of wo********@gmail.com Sent: Thursday, January 06, 2005 11:53 AM To: py*********@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
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
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
<gu***************@gmail.com> 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
On Thu, 6 Jan 2005 12:55:22 +0530, Binu K S <bi*********@gmail.com> 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
On Thu, 6 Jan 2005 13:17:11 +0530, Gurpreet Sachdeva
<gu***************@gmail.com> wrote: On Thu, 6 Jan 2005 12:55:22 +0530, Binu K S <bi*********@gmail.com> 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
On Thu, 6 Jan 2005 14:27:40 +0530, Binu K S <bi*********@gmail.com> 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??? :o)
Please Comment...
Garry http://mail.python.org/pipermail/pyt...er/007650.html
Rest assured you're not on drugs :)
On Thu, 6 Jan 2005 14:45:24 +0530, Gurpreet Sachdeva
<gu***************@gmail.com> wrote: On Thu, 6 Jan 2005 14:27:40 +0530, Binu K S <bi*********@gmail.com> 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??? :o)
Please Comment...
Garry wo********@gmail.com 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: rbt |
last post by:
The below script produces a ' Bad File Descriptor' when
executed. If I remove the try: except: statements, the script stops when
the error occurs.
The purpose of the script is to monitor the...
|
by: lynology |
last post by:
I need help trying to figure why this piece of code gives me a "Bad
File descriptor error" everytime I try to run it and invoke fflush.
This piece of code simple outputs a char string to the output...
|
by: CSN |
last post by:
At startup this appears in the log:
WARNING: dup(0) failed after 3195 successes: Bad file
descriptor
What does it mean?
__________________________________________________
|
by: news |
last post by:
Recently our mail from our e-commerce site has been rejected by AOL due
to an IP block because someone was using our PHP scripts to send spam.
Well, I got that fixed.
But our legitimate...
|
by: Astan Chee |
last post by:
Hi everyone,
I currently have a problem with reading one of my files.
Normally I'd read files like so:
overf = 'C:\\overf'
my_overf = open(overf,'r')
contents = my_overf.readlines()...
|
by: enjoylife27 |
last post by:
Hi there,
I am using Perl 5.8 with Apache 2.2 on Win XP SP2, all configured and working fine. I am able to run perl programs from command prompt. I am also able to call CGI scripts from HTML pages...
|
by: shathil |
last post by:
#!/bin/bash
cat /var/backup/192.168.4.3/dirnum.prn | awk '{print $1}'
if i put above script can read the file.
#!/bin/bash
file1=/var/backup/192.168.4.3/dirnum.prn
exec <$file1
...
|
by: kirubagari |
last post by:
Public Sub ReWrite_Open_File(ByVal FileNo As Long)
Dim FileSize As Long, Buffer() As Byte
' PART 1: Read the file.
FileSize = LOF(FileNo) ' Determine how large the file is (in bytes).
ReDim...
|
by: rajiv07 |
last post by:
Hi i have script for upload file to server.when i execute this script it gives Bad file Descriptor error.could u anybody help on this.
use strict;
use CGI;
use CGI::Carp qw(fatalsToBrowser);...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |