Problem writing file after reading it with pydicom

889 views
Skip to first unread message

Andrei

unread,
Dec 3, 2009, 11:28:24 AM12/3/09
to pydicom
Hello,

I want to change some data in a few dicom files and I do it like this:

dataSet = dicom.read_file(fileName)
...
dataSet[tag].value = aValue
...
dicom.write_file(fileName, dataSet)

When reaching the final statement, I get an error:

2009-12-03 15:00:07 ScriptError: (0) Traceback (most recent call
last):
2009-12-03 15:00:23 ScriptError: MyTool.py(340) error, see below
2009-12-03 15:00:24 ScriptError: MyTool.py(340) dicom.write_file
(fileName, dataSet)
2009-12-03 15:00:24 ScriptError: filewriter.py(248) error, see below
2009-12-03 15:00:24 ScriptError: filewriter.py(248) fp = DicomFile
(filename,'wb')
2009-12-03 15:00:24 ScriptError: filebase.py(152) error, see below
2009-12-03 15:00:24 ScriptError: filebase.py(152) return
DicomFileLike(open(*args, **kwargs))
2009-12-03 15:00:24 ScriptError: filebase.py(152) IOError: [Errno 13]
Permission denied: u'D:/Data/aFile.dcm'

If I use ProcessExplorer (http://technet.microsoft.com/en-us/
sysinternals/bb896653.aspx) to search for file handles I can see that
'aFile.dcm' is still open (!).

I checked and apparently the file should get closed towards the end of
the "dicom.read_file()" method (though not through the original
handle).

Is there a bug in "dicom.read_file()" or am I missing something?

Andrei

Andrei

unread,
Dec 4, 2009, 3:47:52 AM12/4/09
to pydicom
I forgot to mention that I had to modify the read_file() function from
dicom/filereader.py with the suggestion from issue 58 (http://
code.google.com/p/pydicom/issues/detail?id=58#c1). Namely, I replaced
line 409 from:

if type(fp) is type(""):

to:

if isinstance(fp, basestring):

Hope this helps reproducing the error.

Andrei

Andrei

unread,
Dec 4, 2009, 3:51:04 AM12/4/09
to pydicom
...and I did it because othwerwise it would crash on unicode strings,
as reported in Issue 58.
Sorry for the multiple posts.
A.

Darcy Mason

unread,
Dec 4, 2009, 7:13:00 PM12/4/09
to pydicom
On Dec 4, 3:51 am, Andrei <areved...@gmail.com> wrote:
> Sorry for the multiple posts.

No problem -- thank you for giving so much detail. It makes it much
easier to try to diagnose.

> > > When reaching the final statement, I get an error:
...
> > > 2009-12-03 15:00:24 ScriptError: filebase.py(152) IOError: [Errno 13]
> > > Permission denied: u'D:/Data/aFile.dcm'
>
> > > If I use ProcessExplorer (http://technet.microsoft.com/en-us/
> > > sysinternals/bb896653.aspx) to search for file handles I can see that
> > > 'aFile.dcm' is still open (!).
>
> > > I checked and apparently the file should get closed towards the end of
> > > the "dicom.read_file()" method (though not through the original
> > > handle).
>
> > > Is there a bug in "dicom.read_file()" or am I missing something?

I tried to replicate this using code similar to yours (starting from
version 0.9.3 plus the basestring mod, and using a unicode file name),
and haven't been able to so far.

The one possibility that comes to mind is that the file would remain
open if an exception was being thrown part-way through reading, as
discussed in a previous discussion [1] and now on the issues list [2].

Is it possible that an exception is being missed (silenced somehow)
when the read_file() is being done? If something like that is
happening, passing in an already-opened file (DicomFile object) and
catching exceptions as discussed in [1] might help.

It doesn't seem very likely, but that's all I could think of so far.
Regards,
Darcy

[1] http://groups.google.com/group/pydicom/browse_thread/thread/acb86a9facd4afa
[2] http://code.google.com/p/pydicom/issues/detail?id=63

A. R.

unread,
Dec 7, 2009, 12:29:20 PM12/7/09
to pyd...@googlegroups.com
I've found out in the meantime that using "dataSet.save_as(fileName)"
instead of "dicom.write_file(fileName, dataSet)" works.
But now that you wrote me I tried to see if any exceptions get raised
which are not caught but couldn't find anything.
However, a part of my code changed and I found out that even the first
version works! (i.e. "dicom.write_file()")

I think my problem must have originated in a third party module which
I used to check whether a file is DICOM or not.
In the current version I do not use that third party module anymore,
as I completely switched to pydicom. I assume that the aforementioned
module was always leaving the last file open, as the error that I
reported always happened on the last file in a list of files, if I
remember correctly.

Everything happened in the context of MeVisLab, where the troublesome
module was a non-python one.

Many thanks for your quick answer and sorry for the trouble!

Best regards,

Andrei
> --
>
> You received this message because you are subscribed to the Google Groups "pydicom" group.
> To post to this group, send email to pyd...@googlegroups.com.
> To unsubscribe from this group, send email to pydicom+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/pydicom?hl=en.
>
>
Reply all
Reply to author
Forward
0 new messages