473,385 Members | 1,867 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Strange Behavior: csv module & IDLE

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:\testD:\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
Dec 29 '07 #1
3 1498
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:\testD:\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
Dec 29 '07 #2
On Dec 29, 1:12 pm, t_rectenwald <t.rectenw...@gmail.comwrote:
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:\testD:\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.
Dec 29 '07 #3
On Dec 28, 9:43*pm, John Machin <sjmac...@lexicon.netwrote:
On Dec 29, 1:12 pm, t_rectenwald <t.rectenw...@gmail.comwrote:
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:\testD:\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
Dec 29 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Marcus Schneider | last post by:
I use PythonWin on WinXP. Every time I change a module, I have to leave PythonWin and re enter to make it notice I have made changes. I guess this is not the normal way to do that.. do I have to...
6
by: Anders Eriksson | last post by:
Hello! I'm using ActivePython 2.3.2 build 232 on Windows 2000 and I have noticed a strange behavior in PythonWin IDE (win32all build 163) I'm from Sweden and we have a couple of letters in our...
0
by: Bill Davy | last post by:
I am working with MSVC6 on Windows XP. I have created an MSVC project called SHIP I have a file SHIP.i with "%module SHIP" as the first line (file is below). I run SHIP.i through SWIG 1.3.24...
2
by: Gopal | last post by:
Hi, I've a module report.py having a set of funtions to open/close/write data to a log file. I invoke these functions from another module script.py. Whenever I'm changing something in...
0
by: Jeremy Moles | last post by:
Hey guys. I have an extension module written in C that abstracts and simplifies a lot of what we do here. I'm observing some strange behavior and wanted to know if anyone had any advice as to how I...
6
by: robert | last post by:
I get python crashes and (in better cases) strange Python exceptions when (in most cases) importing and using cookielib lazy on demand in a thread. It is mainly with cookielib, but remember the...
5
by: rconradharris | last post by:
A co-worker of mine came across some interesting behavior in the Python interpreter today and I'm hoping someone more knowledgeable in Python internals can explain this to me. First, we create...
2
by: darkyng | last post by:
hi there,...my name s roberto,from italy...ehm....please...be patient for my english....however i have a question....im trying to know...why this strange thing appens....what?well look at this: ...
5
by: dg.google.groups | last post by:
Hi all, Is there any standard way to tell if the user is running from a module or from an interactive shell like IDLE or IPython? The best I've come up with so far is for a function to look at...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.