[python-win32] I/O error

Preston Landers planders at gmail.com
Mon Mar 22 22:48:21 CET 2010


Your indentation is a bit hard to read in this format, but it looks like the
"for subkey in uninstall" block needs to be inside the "with open" block.
When the "with open" block exits, the file is no longer open.  Thus writing
to a closed file will fail.


On Mon, Mar 22, 2010 at 4:22 PM, travel europe <europe_travel at hotmail.com>wrote:

>  Hello,
>
> I am getting the following error "ValueError: I/O operation on closed file"
> when running a module to pull data from the Windows registry.
>
> The code is as follows:
>
> <code>
> import csv
> from winsys import registry
>
> key =
> r"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
> fields = ["DisplayName", "Comments", "InstallDate"] # add to as required
>
> uninstall = registry.registry (key)
> with open ("installed.txt", "wb") as f:
>  writer = csv.writer (f)
>  writer.writerow (fields)
> for subkey in uninstall:
>  info = dict (subkey.values ())
>  writer.writerow ([info.get (field) for field in fields])
> <code>
>
> <error message>
> The error message is as follows:
> Traceback (most recent call last):
>   File "C:\Python26\tim.py", line 13, in <module>
>     writer.writerow ([info.get (field) for field in fields])
> ValueError: I/O operation on closed file
> <error message>
>
> Any ideas on how to rectify this problem? Thanks!
>
> ------------------------------
> Hotmail is redefining busy with tools for the New Busy. Get more from your
> inbox. Sign up now.<http://www.windowslive.com/campaign/thenewbusy?ocid=PID27925::T:WLMTAGL:ON:WL:en-US:WM_HMP:032010_2>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-win32/attachments/20100322/c82d0227/attachment.html>


More information about the python-win32 mailing list