[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Strange Behavior: csv module & IDLE

t_rectenwald

12/29/2007 2:13:00 AM

I've noticed an oddity when running a program, using the csv module,
within IDLE. I'm new to Python so am confused by what is happening.
Here is what I'm doing:

1) Open the IDLE Shell.
2) Select File | Open...
3) Choose my file, foo.py, opening it in a window.
4) From that window, I hit F5 to run the module.

Within the program, the snippet where I use the csv module is below:

==============================
csvfile = open('foo.csv', 'w')
writer = csv.writer(csvfile)

for row in rows:
writer.writerow(row[0:3])

csvfile.close
==============================

The rows object is returned from a database query and is a list of
tuples. Now here is the strange thing. If I run this program
directly from the command line, i.e.,

D:\test> D:\python25\python foo.py

It runs fine, foo.csv is created and all is well. However, when I run
it through the IDLE shell as described above, the foo.csv file is
created but remains empty at 0 bytes. When I try to delete the file,
Windows says it is in use. The only way I can break out of this is by
restarting the IDLE shell. In other words, it appears that the shell
is hanging.

This will run through Task Scheduler, so shouldn't be a problem, but
I'm worried that I'm coding this wrong for it to be acting this way
under IDLE. Any help or explanation would be appreciated.

Best Regards,
Tom
3 Answers

Marc 'BlackJack' Rintsch

12/29/2007 2:37:00 AM

0

On Fri, 28 Dec 2007 18:12:58 -0800, t_rectenwald wrote:

> Within the program, the snippet where I use the csv module is below:
>
> ==============================
> csvfile = open('foo.csv', 'w')
> writer = csv.writer(csvfile)
>
> for row in rows:
> writer.writerow(row[0:3])
>
> csvfile.close
> ==============================
>
> The rows object is returned from a database query and is a list of
> tuples. Now here is the strange thing. If I run this program
> directly from the command line, i.e.,
>
> D:\test> D:\python25\python foo.py
>
> It runs fine, foo.csv is created and all is well. However, when I run
> it through the IDLE shell as described above, the foo.csv file is
> created but remains empty at 0 bytes. When I try to delete the file,
> Windows says it is in use. The only way I can break out of this is by
> restarting the IDLE shell. In other words, it appears that the shell
> is hanging.
>
> This will run through Task Scheduler, so shouldn't be a problem, but
> I'm worried that I'm coding this wrong for it to be acting this way
> under IDLE. Any help or explanation would be appreciated.

You are not closing the file so the buffered data is not written to disk.
To call a function you need the parenthesis, otherwise you are just
referencing it without any effect.

Ciao,
Marc 'BlackJack' Rintsch

John Machin

12/29/2007 2:43:00 AM

0

On Dec 29, 1:12 pm, t_rectenwald <t.rectenw...@gmail.com> wrote:
> I've noticed an oddity when running a program, using the csv module,
> within IDLE. I'm new to Python so am confused by what is happening.
> Here is what I'm doing:
>
> 1) Open the IDLE Shell.
> 2) Select File | Open...
> 3) Choose my file, foo.py, opening it in a window.
> 4) From that window, I hit F5 to run the module.
>
> Within the program, the snippet where I use the csv module is below:

Forget snippet, show us a *whole* "program". Cut out the database
stuff; just use some simple made-up value for "rows".


> ==============================
> csvfile = open('foo.csv', 'w')

Always use 'wb' -- not the cause of the current problem but it will
bite you later.

> writer = csv.writer(csvfile)
>
> for row in rows:
> writer.writerow(row[0:3])
>

Adding

del writer

may help

> csvfile.close

The above statement does nothing. You meant csvfile.close(), I
presume.


> ==============================
>
> The rows object is returned from a database query and is a list of
> tuples. Now here is the strange thing. If I run this program
> directly from the command line, i.e.,
>
> D:\test> D:\python25\python foo.py
>
> It runs fine, foo.csv is created and all is well. However, when I run
> it through the IDLE shell as described above, the foo.csv file is
> created but remains empty at 0 bytes. When I try to delete the file,
> Windows says it is in use. The only way I can break out of this is by
> restarting the IDLE shell. In other words, it appears that the shell
> is hanging.

No it's not hanging, it's just that the file is still open; you
haven't closed it. It won't be closed until you exit IDLE.

>
> This will run through Task Scheduler, so shouldn't be a problem, but
> I'm worried that I'm coding this wrong for it to be acting this way
> under IDLE. Any help or explanation would be appreciated.
>

Do these things inside a function, so that the objects get garbage-
collected on exit.

t_rectenwald

12/29/2007 3:21:00 AM

0

On Dec 28, 9:43 pm, John Machin <sjmac...@lexicon.net> wrote:
> On Dec 29, 1:12 pm, t_rectenwald <t.rectenw...@gmail.com> wrote:
>
> > I've noticed an oddity when running a program, using the csv module,
> > within IDLE.  I'm new to Python so am confused by what is happening.
> > Here is what I'm doing:
>
> > 1) Open the IDLE Shell.
> > 2) Select File | Open...
> > 3) Choose my file, foo.py, opening it in a window.
> > 4) From that window, I hit F5 to run the module.
>
> > Within the program, the snippet where I use the csv module is below:
>
> Forget snippet, show us a *whole* "program". Cut out the database
> stuff; just use some simple made-up value for "rows".
>
> > ==============================
> > csvfile = open('foo.csv', 'w')
>
> Always use 'wb' -- not the cause of the current problem but it will
> bite you later.
>
> > writer = csv.writer(csvfile)
>
> > for row in rows:
> >     writer.writerow(row[0:3])
>
> Adding
>
> del writer
>
> may help
>
> > csvfile.close
>
> The above statement does nothing. You meant csvfile.close(), I
> presume.
>
> > ==============================
>
> > The rows object is returned from a database query and is a list of
> > tuples.  Now here is the strange thing.  If I run this program
> > directly from the command line, i.e.,
>
> > D:\test> D:\python25\python foo.py
>
> > It runs fine, foo.csv is created and all is well.  However, when I run
> > it through the IDLE shell as described above, the foo.csv file is
> > created but remains empty at 0 bytes.  When I try to delete the file,
> > Windows says it is in use.  The only way I can break out of this is by
> > restarting the IDLE shell.  In other words, it appears that the shell
> > is hanging.
>
> No it's not hanging, it's just that the file is still open; you
> haven't closed it. It won't be closed until you exit IDLE.
>
>
>
> > This will run through Task Scheduler, so shouldn't be a problem, but
> > I'm worried that I'm coding this wrong for it to be acting this way
> > under IDLE.  Any help or explanation would be appreciated.
>
> Do these things inside a function, so that the objects get garbage-
> collected on exit.

Thanks for all of the help. I'm still learning Python so dorked up
here and didn't add the empty parenthesis around csvfile.close as I
should have. So, it never called the close() function, but just
referenced it as was noted in your responses. After doing that,
everything works fine and the file is closed properly. I do have this
in a function in the actual script I'm writing, just sort of made a
dummy, foo.py, to do some testing and didn't have it in a function
there.

I'll research "wb" now to figure out what that does. Thanks again for
the help!
Tom